Beginner · Linux and the shell · topic 4 of 57 · 35 min
Working with files: ls, cp, mv, rm, links and find
Explanation
Listing files
$ ls -l /etc/hosts
-rw-r--r--. 1 root root 158 Jun 23 2020 /etc/hosts
$ ls -la ~
drwx------. 3 asha asha 95 Oct 19 10:01 .
drwxr-xr-x. 4 root root 32 Oct 18 09:12 ..
-rw-------. 1 asha asha 512 Oct 19 10:05 .bash_history
-rw-r--r--. 1 asha asha 18 Oct 18 09:12 .bash_profile
drwxr-xr-x. 2 asha asha 6 Oct 19 10:01 reportsIn the long form, the first letter is the type: - for a normal file, d for a directory, l for a symbolic link. Then come the permissions (next module), the link count, the owner, the group, the size, the date changed, and the name. Useful options: -h (sizes like 1.2M), -t (newest first), -r (reverse), -R (recursive), -d (the directory itself, not its contents).
Creating, copying, moving, deleting
$ mkdir -p projects/2026/reports # -p also creates the missing parent folders
$ touch notes.txt # creates an empty file (or updates its time)
$ cp notes.txt notes.bak # copy a file
$ cp -r projects projects-old # copy a directory and everything inside (-r = recursive)
$ cp -a /etc/ssh /root/ssh-backup # -a = archive: keep owners, permissions, times
$ mv notes.bak old-notes.txt # rename
$ mv old-notes.txt projects/ # move into a directory
$ rm projects/old-notes.txt # delete a file
$ rmdir projects/2026/reports # delete an EMPTY directory
$ rm -r projects-old # delete a directory and all inside itWildcards (globbing) let one name match many files. The shell expands them before the command runs: * matches anything, ? matches one character, and [abc] matches one of those characters. ls *.log lists all files ending in .log; rm report?.txt removes report1.txt to report9.txt. Braces make lists: mkdir dir{1..3} creates dir1, dir2 and dir3.
Hard links and symbolic (soft) links
A file's data lives in an inode, a record on disk with the owner, permissions and where the data blocks are. A filename is just a pointer to an inode. A hard link is a second name for the same inode: both names are equal, and the data stays until the last name is deleted. A symbolic link (symlink, soft link) is a small file that holds a path to another file, like a shortcut.
$ ln report.txt report-hard.txt # hard link
$ ln -s /var/log/httpd logs # symbolic link named "logs"
$ ls -li report.txt report-hard.txt logs
33575012 -rw-r--r--. 2 asha asha 120 Oct 19 10:30 report.txt
33575012 -rw-r--r--. 2 asha asha 120 Oct 19 10:30 report-hard.txt
33575019 lrwxrwxrwx. 1 asha asha 14 Oct 19 10:31 logs -> /var/log/httpd| Hard link | Symbolic link | |
|---|---|---|
| Points to | The inode (the data itself) | A path (a name) |
| Works for directories | No | Yes |
| Works across file systems | No, same file system only | Yes |
| If the original is deleted | Data still there through the other name | Link breaks (dangling) |
find: searching the tree
$ find /etc -name "*.conf" # by name (quote the pattern!)
$ find /home -user asha # owned by asha
$ find /var/log -size +100M # bigger than 100 MiB
$ find /tmp -mtime +7 # changed more than 7 days ago
$ find / -perm -4000 -type f 2>/dev/null # files with the SUID bit; hide errors
$ find /home -user asha -exec cp -a {} /root/asha-files/ \;find searches live, so it is slow on a whole disk but always current. locate (from the mlocate/plocate package) searches a database and is fast but may be out of date until updatedb runs.
Common mistakes
- find /etc -name *.conf without quotes: the shell expands *.conf in your current directory first, and find gets the wrong pattern. Always quote.
- cp -r instead of cp -a when backing up config: you lose the owner and permissions. Use -a for backups.
- Making a symlink with a relative target from the wrong directory, so it points nowhere. ls -l shows the target; check it.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
Working with files: ls, cp, mv, rm, links and find · 1/7
In this lesson you will create, copy, move, delete and find files.
Prefer a video by a teacher? Find free videos on “linux cp mv rm find command hard link soft link tutorial” on YouTube ↗
In real life: Cleaning old reports at a GST filing office
A tax-filing firm in Delhi generates PDF reports on a RHEL server every night. The disk fills up every quarter. The admin first lists reports older than 90 days, checks the list, and then moves them to an archive disk instead of deleting them, because clients sometimes ask for old reports.
$ find /srv/reports -name "*.pdf" -mtime +90 | wc -l
1843
$ mkdir -p /archive/reports
$ find /srv/reports -name "*.pdf" -mtime +90 -exec mv {} /archive/reports/ \;
$ df -h /srv
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vgdata-srv 50G 21G 30G 42% /srvPractice (0/3 done)
What does mv report.txt final.txt do?
Which line has the bug? (Goal: find all .conf files under /etc.) 1: cd /home/asha 2: find /etc -name *.conf 3: echo done
In your lab: create ~/lab/{a,b,c}, put a file in each, make a hard link and a symlink to one file, then use find to list every file in ~/lab owned by you and changed in the last day.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Reading text: grep, less, pipes and redirectionLinux and the shell · same module
- The Linux filesystem and everyday commandsDevOps: Git, Docker, Kubernetes and CI/CD · Linux for DevOps
- Contexts and file labels: ls -Z, semanage fcontext and restoreconSELinux and SSH
- UPDATE and DELETE safelySQL and Databases · Changing data, transactions and ACID
- Access control lists (ACLs)Permissions, users and sudo
- Linux basics for backend developersBackend Development · Deployment and scaling
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
Which command copies the folder /etc/ssh with all owners and permissions kept?
Answer: cp -a /etc/ssh /backup
-a (archive) copies recursively and keeps owner, permissions and times.
You delete the original file. Which kind of link still shows the data?
Answer: Hard link
A hard link points to the same inode, so the data stays until the last name is removed.
Which find option matches files bigger than 1 GiB?
Answer: -size +1G
-size +1G means larger than 1 GiB.
Which directory listing type letter means a symbolic link?
Answer: l
l = link, d = directory, - = regular file.
What does mkdir -p a/b/c do when a does not exist?
Answer: Creates a, a/b and a/b/c
-p creates every missing parent directory.
What does mkdir -p /opt/app/releases/v2 do if /opt/app does not exist yet?Related
Answer: Creates all missing folders in the path
The -p flag creates every missing parent folder and does not complain if they exist.
A file moved with mv from /home/asha to /var/www/html gives 403. Why?Related
Answer: mv keeps the old user_home_t label
Moved files keep their context; run restorecon.
From Contexts and file labels: ls -Z, semanage fcontext and restorecon
Why do many apps use a soft delete (is_active = 0) for customers?Related
Answer: Old orders, invoices and audits still need the row
The row is hidden from normal use but kept for history.
Which command shows the ACL of a file?Related
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 2 more topics in “Linux and the shell” before its exam: Reading text: grep, less, pipes and redirection, Editing files with vim.
- Module 1 exam: Linux and the shell · 13 questions · 20 min, without help. 6 topics of this module still to finish first.
- Then the next module, Permissions, users and sudo. 57 topics left in the course before the final exam.