CertNote/ Commute Study Notes
Roadmap
KOEN
CLF-C02 · FoundationalCloud Practitioner - Foundational
DVA-C02 · AssociateDeveloper - Associate
SAA-C03 · AssociateSolutions Architect - Associate
SOA-C02 · AssociateCloudOps Engineer - Associate
SAP-C02 · ProfessionalSolutions Architect - Professional
DOP-C02 · ProfessionalDevOps Engineer - Professional
Linux Master 2 · 2급KAIT Linux Master - Level 2
  • Week 1
    • 1.Operating Systems and the Kernel: What Linux Does
    • 2.The Birth of Linux: From Unix to Linus
    • 3.Free Software and Open Source: What a License Decides
    • 4.Distributions: The Unit That Bundles the Kernel and Tools for Sale
    • 5.Week 1 Review: The Identity Behind the Name Linux
  • Week 2
  • Week 3
    • 1.First Steps in the Shell: How to Read a Single Command Line
    • 2.Working with Directories and Files
    • 3.Viewing and Finding Contents
    • 4.User and Account Management
    • 5.Week 3 Review: The Commands on One Page
  • Week 4
  • Week 5
  • Week 6
  • Week 7
  • Week 8
  • Week 9
  • Week 10
  • Week 11
SCS-C03 · SpecialtySecurity - Specialty
Linux Master 1 · 1급KAIT Linux Master - Level 1
MLA-C01 · AssociateMachine Learning Engineer - Associate
AIF-C01 · FoundationalAI Practitioner - Foundational
DEA-C01 · AssociateData Engineer - Associate
MLS-C01 · SpecialtyMachine Learning - Specialty
SQLD · 개발자SQL Developer (SQLD)

© CertNote · 주간랩스

  • About
  • Changelog
  • Contact
  • Terms
  • Privacy
← Linux Master 2/Week 3/Day 2

Based on the Linux Master 2 exam syllabus·Report an error

Linux Master 2· AssociateWeek 3 · Day 2~20 min read

Day 2 - Working with Directories and Files

📌 Key Takeaways

  • Three commands for dealing with location: pwd (where am I) · cd (move) · ls (what's here).
  • ls -a shows hidden files and ls -l shows detailed info. A hidden file in Linux is a file whose name starts with ..
  • cp copies, and mv both moves and renames. There is no separate rename command for mv.
  • To handle directories you need -r (recursive) — cp -r, rm -r.
  • rm has no recycle bin. Once it's deleted, it's gone. rm -rf is especially dangerous.

Where Am I Now

CommandWhat it does
pwdPrints the absolute path of the current directory (print working directory)
cd pathMove to that path
cd or cd ~To the home directory
cd ..To the parent directory
cd -To the directory you were in just before
pwd            # /home/user
cd /etc        # move using an absolute path
cd ..          # go up to /
cd ~           # return home
  • cd accepts both absolute and relative paths. When you are in /home/user, cd docs and cd /home/user/docs go to the same place.
  • cd - remembers the previous location and goes back to it, which is handy when you work by hopping between two directories.
  • Adding a user name after ~ means that user's home — cd ~alice goes to /home/alice.
[user@localhost ~]$ cd /var/log
[user@localhost log]$ cd /etc
[user@localhost etc]$ cd -
/var/log
[user@localhost log]$ pwd
/var/log

What's Here — ls

OptionMeaning
-aInclude hidden files (all)
-lDetailed info (long) — permissions, owner, size, time
-hHuman-readable sizes (human) — KB, MB
-tSort by time
-rReverse order (reverse)
-RRecursively through subdirectories
-iShow the inode number
ls -l          # in detail
ls -la         # in detail, including hidden files
ls -lh         # sizes in KB/MB
ls -ltr        # time order, reversed — most recent file at the bottom

A few more options are worth knowing, since they make it easier to tell apart the answer choices on exam questions.

OptionMeaning
-dShows the directory itself, not what's inside it (ls -ld /etc)
-SSort by size (largest first)
-FAppends a type marker after the name — directory /, executable *, symbolic link @
-ASame as -a but leaves out . and ..

⚠️ Different case means a different option. -r is reverse, -R is recursive. -a shows everything including . and .., while -A omits those two. -s shows the block count, and -S sorts by size.

  • A plain ls with no options sorts by name. -t puts most recently modified first, -S puts largest first, and adding -r flips each of them.
  • The . and .. that always appear with ls -a are entries pointing to the current directory and the parent directory. Since their names start with ., they are hidden too.

Hidden Files

  • In Linux, a hidden file is a file whose name starts with .. It is not a separate attribute.
  • Most configuration files, such as .bashrc, .ssh, and .profile, fall into this category.
  • They don't show up with a plain ls; you need ls -a to see them.

Reading One Line of ls -l

   -rw-r--r--  1  user  group  1024  Sep  3 09:00  a.txt
   │        │  │   │      │      │        │          │
   │        │  │   │      │      │        │          └── name
   │        │  │   │      │      │        └───────────── last modified time
   │        │  │   │      │      └────────────────────── size (bytes)
   │        │  │   │      └───────────────────────────── group
   │        │  │   └──────────────────────────────────── owner
   │        │  └──────────────────────────────────────── hard link count
   │        └─────────────────────────────────────────── permissions (details next week)
   └──────────────────────────────────────────────────── file type

The very first character is the file type.

CharacterType
-Regular file
dDirectory
lSymbolic link
bBlock device (disk)
cCharacter device (keyboard)

Let's look at how each type appears in real output, all at once.

$ ls -l /dev/sda /dev/tty /etc/passwd /bin /tmp -d
lrwxrwxrwx.  1 root root         7 Sep  3 09:00 /bin -> usr/bin
brw-rw----.  1 root disk    8,   0 Sep  3 09:00 /dev/sda
crw-rw-rw-.  1 root tty     5,   0 Sep  3 09:00 /dev/tty
-rw-r--r--.  1 root root      2104 Sep  3 09:00 /etc/passwd
drwxrwxrwt. 12 root root      4096 Sep  3 09:10 /tmp
  • A symbolic link has -> original after its name, and its size is the number of characters in the path it points to (usr/bin is 7 characters).
  • A device file shows two numbers (major number, minor number) in the size column. A directory's size is not the total of the files inside it but the size of the directory entry itself.
  • The time that ls -l shows is the last modified time (mtime). A file carries three timestamps.
TimeChanges whenHow to view
mtime (modify)The file's contents are editedls -l
atime (access)The file is readls -lu
ctime (change)Attributes such as permissions or owner changels -lc

Pitfall: ctime is not the creation time (create). It is the time the status changed (change). Editing the contents changes both mtime and ctime, while just running chmod changes only ctime.

Creating and Deleting

CommandWhat it doesNote
mkdir nameCreate a directoryFails if intermediate paths are missing
mkdir -p a/b/cCreate intermediate paths too, in one go
rmdir nameDelete a directoryOnly works if it is empty
rm fileDelete a fileCannot be undone
rm -r directoryDelete a directory along with its contents
rm -fForce delete without confirmation
touch fileCreate an empty file / update timestamps

⚠️ rm -rf is the most dangerous command in Linux. With no recycle bin, recovery is practically impossible. It is safer to get in the habit of checking with ls first before deleting a path.

If you try it yourself, the error messages show what is required.

$ mkdir project/src/lib
mkdir: cannot create directory 'project/src/lib': No such file or directory
$ mkdir -p project/src/lib
$ touch project/src/lib/a.c
$ rmdir project
rmdir: failed to remove 'project': Directory not empty
$ rm project
rm: cannot remove 'project': Is a directory
$ rm -r project
OptionMeaning
rm -iAsks one by one before deleting (interactive)
rm -fDoesn't ask, and gives no error even if the file doesn't exist (force)
rm -rDirectories including their contents (recursive)
rm -vPrints the items that were deleted (verbose)
rmdir -p a/b/cAfter removing c, also removes the parent directories that became empty, in order
  • When -i and -f are used together, the one written last wins. RedHat-family systems often alias rm to rm -i for root, which is why root gets a confirmation prompt when typing rm.
  • If the file doesn't exist, touch creates an empty (0-byte) file; if it exists, it leaves the contents alone and just sets the time to now. You can also specify the time directly with -t, like touch -t 202609031200 a.txt.

Wildcards — Pointing at Many Files at Once

SymbolWhat it matchesExample
*Any string of zero or more characters*.txt → a.txt, note.txt
?Exactly one charactera?.txt → a1.txt (not a10.txt)
[abc]One of the characters in the brackets[ab].txt → a.txt, b.txt
[a-z]One character in the rangefile[0-9] → file1
[!abc]One character that is not in the brackets[!a].txt → b.txt
ls *.txt             # everything ending in .txt
rm report?.bak       # report1.bak, reportA.bak ...
cp [a-c]*.conf /tmp  # .conf files starting with a, b, or c
  • Wildcards are expanded first by the shell, not the command, turned into a list of file names, and then passed to the command. That is why you can check what gets matched with ls * before running rm *.
  • * does not match hidden files (those starting with .). Even after rm *, .bashrc remains.

Copying and Moving

CommandWhat it does
cp source copyCopy
cp -r directory targetCopy a directory — -r is required
cp -iAsks before overwriting
cp -pCopies while preserving permission and time info
mv source targetMove
mv oldname newnameRename
  • A single mv serves as both move and rename. Linux has no dedicated rename command by default.
  • Running mv a.txt b.txt within the same directory renames it, and giving a different directory as the target moves it.
  • mv doesn't need -r. Directories are simply moved.
cp a.txt b.txt          # copy a file
cp -r docs backup       # copy a directory (error without -r)
mv a.txt /tmp/          # move
mv old.txt new.txt      # rename

The Result Differs When the Target Already Exists

cp and mv behave differently depending on whether the last argument is an existing directory.

CommandWhen backup doesn't existWhen backup is an existing directory
cp -r docs backupA copy of docs is created under the name backupIt goes inside as backup/docs
mv docs backupdocs is renamed to backupIt is moved to backup/docs
cp a.txt b.txt c.txt /tmp/     # with several sources, the last one must be a directory
mv *.log /var/backup/          # move many files at once
  • If a file with the same name exists at the target, it is overwritten without asking. Use -i to prevent this.
Optioncpmv
-iConfirm before overwritingConfirm before overwriting
-fForce without askingForce without asking
-vPrint what was processedPrint what was processed
-uCopy only when the source is newerMove only when the source is newer
-bKeep a backup of the file being overwrittenKeep a backup of the file being overwritten
-r / -RDirectories including subdirectories(not needed)
-pPreserve permissions, owner, and time(a move preserves them already)
-aA backup-oriented option: -r plus preserving attributes and links—

The fundamental difference between cp and mv. cp creates a new file — so the copy's owner becomes the person who copied it and its time becomes now (use -p to keep the original's). Within the same file system, mv doesn't move the data; it just re-attaches the name tag. Because the inode stays the same, even a large directory moves instantly, which is why -r isn't needed.

Links

KindCommandNature
Hard linkln source linkPoints to the same inode. The data survives even if the original is deleted
Symbolic linkln -s source linkA shortcut that points to a path. It breaks if the original is deleted
  • A symbolic link shows l as the first character in ls -l and appears in the form link -> original.
  • A hard link cannot be made across different file systems, and cannot be made to a directory.

A file name is really a name tag attached to a number called an inode. The actual data, along with permission, owner, and size information, is held by the inode. The difference between the two kinds of links comes from this.

$ echo hello > a.txt
$ ln a.txt hard.txt
$ ln -s a.txt soft.txt
$ ls -li
131075 -rw-r--r--. 2 user user 6 Sep  3 09:00 a.txt
131075 -rw-r--r--. 2 user user 6 Sep  3 09:00 hard.txt
131090 lrwxrwxrwx. 1 user user 5 Sep  3 09:01 soft.txt -> a.txt
$ rm a.txt
$ cat hard.txt
hello
$ cat soft.txt
cat: soft.txt: No such file or directory
  • a.txt and hard.txt have the same inode number and a link count of 2. They are not an original and a copy but two equal names for the same file.
  • rm removes one name tag. The data is erased only when the link count reaches 0, so even after deleting a.txt, you can still read the contents through hard.txt.
  • soft.txt has its own inode, and its content is only the path text a.txt. When the original disappears, it becomes a broken link with nothing to point to.
ComparisonHard linkSymbolic link
inodeSame as the originalHas its own
First character of ls -l- (indistinguishable from a regular file)l
When the original is deletedContents surviveIt breaks
Across file systemsNot possiblePossible
Directory targetNot possiblePossible
SizeSame as the originalNumber of characters in the path it points to

Tomorrow we will learn the commands for looking inside files and finding what you want.

📖 Glossary

  • Working directory : The directory the shell is currently in. Check it with pwd.
  • Hidden file : A file whose name starts with .. Visible only with ls -a.
  • Recursive (-r) : An option that walks through subdirectories as well. Needed to handle directories with cp and rm.
  • Hard link : Another name that points to the same inode.
  • Symbolic link : A shortcut file that holds the path of another file. Created with ln -s.
  • touch : A command that creates an empty file or updates the time info of an existing file.
  • mkdir -p : An option that also creates intermediate paths if they are missing.

📝 Practice Questions

Click a choice to reveal the answer and explanation.

Question 1

Which of the following options is used to list files including hidden files?

Question 2

Which of the following options is absolutely required to copy an entire directory?

Question 3

Which of the following commands is used to rename a file?

Question 4

What does it mean when the first character of an ls -l result is d?

Question 5

Which of the following commands is used to create several levels of directories at once?

PreviousFirst Steps in the Shell: How to Read a Single Command LineWeek 3 · Day 1Next Viewing and Finding ContentsWeek 3 · Day 3

Official references

  • Linux Master 2 official exam syllabus(opens in a new tab)

On this page

  • Key Takeaways
  • Where Am I Now
  • What's Here — ls
  • Hidden Files
  • Reading One Line of `ls -l`
  • Creating and Deleting
  • Wildcards — Pointing at Many Files at Once
  • Copying and Moving
  • The Result Differs When the Target Already Exists
  • Links
  • Glossary
  • Practice Questions