Three commands do almost all of your moving around on a Linux server: pwd tells you where you are, ls shows what is there, and cd takes you somewhere else. This guide covers each one, the path shortcuts that save typing, and the ls options that help you find the biggest or newest files.
Before you start
- SSH access to your Linux VPS. See Connect to your Linux VPS with SSH.
- None of these commands change anything, so you can experiment freely.
Know where you are with pwd
pwd stands for print working directory. It shows the full path of the folder you are in:
pwd/home/deployYour prompt often shows the folder too, but it may shorten it. pwd always prints the complete path.
Absolute and relative paths
A path tells a command where a file or folder is. There are two kinds:
- Absolute paths start with
/and describe the location from the top of the tree./var/log/nginxmeans the same thing no matter where you are. - Relative paths do not start with
/. They are read from your current folder. If you are in/var, thenlog/nginxpoints to/var/log/nginx.
Some shortcuts work in any path:
~is your home folder, for example/home/deployor/root..is the current folder...is the parent folder, one level up.-(withcdonly) is the folder you were in before.
Move around with cd
cd /var/log # go to an absolute path
cd nginx # go into a subfolder of where you are
cd .. # up one level
cd ../.. # up two levels
cd ~ # back to your home folder
cd # also back home
cd - # jump back to the previous foldercd - is useful when you switch between two folders, such as a config folder and a log folder.
Tip: Press Tab while typing a path. The shell completes folder names for you and shows the options if more than one matches. It is faster and prevents typos.
If a folder name contains spaces, quote it or escape the space:
cd "/srv/My Site"
cd /srv/My\ SiteList files with ls
On its own, ls prints the names in the current folder. You can also give it a path:
ls
ls /etc/nginxThe options you will use most:
-l: long format, with permissions, owner, group, size and modification date.-a: show hidden files, whose names start with a dot (such as.bashrcor.ssh).-h: human-readable sizes (4.0K,12M,1.1G) when combined with-l.-d: show the folder itself rather than its contents.
Most people combine them:
ls -lahA typical line looks like this:
-rw-r--r-- 1 deploy deploy 3.7K Nov 2 10:14 .bashrcFrom left to right: the file type and permissions, the link count, the owner, the group, the size, the modification date and the name. A line starting with d is a folder; a line starting with l is a symbolic link.
Sort by size or date
These options help you find what changed recently or what takes up space:
ls -lt # newest first
ls -ltr # oldest first, newest at the bottom
ls -lS # largest first
ls -lSh # largest first, readable sizesls -ltr is handy in a log folder, because the file that was written most recently appears just above your prompt.
Note: For a folder, ls -l shows the size of the folder entry itself, not of everything inside it. To see how much space a folder really uses, run du -sh /path/to/folder.
See the tree at a glance
To see folders inside folders, ls -R lists everything recursively, but the output gets long fast. The tree program is easier to read. Install it on Ubuntu and Debian with:
sudo apt install treeOn AlmaLinux and Rocky Linux:
sudo dnf install treeThen limit the depth so it stays readable:
tree -L 2 /etc/nginxCheck it works
Run this sequence and compare each result with what you expect:
cd /var/log
pwd
ls -ltr | tail -5
cd -
pwd
ls -la ~You should land in /var/log, see the five most recently changed logs, jump back to where you started, and then see the hidden files in your home folder.
Troubleshooting
No such file or directory: check the spelling and case.Documentsanddocumentsare different. Use Tab completion to be sure.Permission deniedwhen youcdinto a folder: your user lacks access. Root-only folders such as/rootneedsudo -i, or list them withsudo ls /root.Not a directory: you tried tocdinto a file. Usels -lto check what the item is.- Hidden files do not show: add
-a. Plainlsnever shows names that start with a dot. - A path with spaces fails: wrap it in quotes, as shown above.
Next steps
- Learn what the main folders hold: The Linux directory structure explained.
- Start working with files: Copy, move and rename files in Linux.
- Read files once you find them: View file contents in Linux.
