$ run as a normal user# run as root (or with sudo)
Use options and arguments
Most commands look like this: command -options arguments. Options change how the command works; arguments say what it works on.
Know the most useful ls options
A few options of ls are used every day.
| Option | Long form | What it does |
|---|---|---|
-a | --all | Also show hidden files (names with a dot) |
-A | --almost-all | Like -a, but without . and .. |
-d | --directory | Show the directory itself, not its contents (use with -l) |
-F | --classify | Add a sign: / for a directory, * for a program |
-h | --human-readable | Sizes as K, M, G (with -l) |
-l | Long format: one file per line with details | |
-r | --reverse | Reverse the order |
-S | Sort by size | |
-t | Sort by time of the last change |
Read the long format
Each line of ls -l has the same columns: type and rights, links, owner, group, size, date, name.
ls -l report.txt
-rw-r--r--. 1 me me 3576 Nov 6 10:15 report.txt| Column | Example | Meaning |
|---|---|---|
| 1 | -rw-r--r-- | Type (- file, d directory, l link) + access rights |
| 2 | 1 | Number of hard links |
| 3 | me | Owner of the file |
| 4 | me | Group of the file |
| 5 | 3576 | Size in bytes |
| 6 | Nov 6 10:15 | Date and time of the last change |
| 7 | report.txt | Name |
The rights are explained in Permissions and users.
Find out the file type with file
The name does not say what a file really is. file looks inside and tells you.
file picture.jpg
picture.jpg: JPEG image data, JFIF standard 1.01
file /bin/ls
/bin/ls: ELF 64-bit LSB pie executable, x86-64, dynamically linked
file /etc/passwd
/etc/passwd: ASCII textRemember
"Everything is a file" in Unix. Pictures, programs, devices and settings are all files. Many of them are plain text you can read.
Read text files with less
less shows a text file one screen at a time. You can scroll and search. Press q to quit.
less /etc/passwd| Key | Action |
|---|---|
Page Up / b | One page back |
| Page Down / Space | One page forward |
| Up / Down | One line |
G | Go to the end |
g or 1G | Go to the start |
/text | Search forward for "text" |
n | Next match |
h | Help |
q | Quit |
Take a tour of the system directories
Linux follows the Filesystem Hierarchy Standard (FHS). The same folders have the same job on almost every distribution.
| Directory | What is in it |
|---|---|
/ | The root of the tree |
/bin, /sbin | Programs; on new systems links to /usr/bin and /usr/sbin |
/boot | The kernel and the boot loader files |
/dev | Device files (disks, terminals …) |
/etc | Settings of the whole system, all plain text |
/home | One home directory per user |
/lib, /lib64 | Shared libraries; on new systems links into /usr |
/lost+found | Pieces saved by a disk check after a crash |
/media, /mnt | Mount points for USB sticks, DVDs and other disks |
/opt | Extra software, often from vendors |
/proc, /sys | Virtual files: live information from the kernel |
/root | Home directory of the superuser |
/run | Runtime data since the last boot |
/tmp | Temporary files, often emptied at reboot |
/usr | Programs, libraries and data of the system; /usr/local for your own installs |
/var | Data that changes: logs in /var/log, mail, databases, caches |
At work
When something goes wrong on a server, look in /var/log first. Settings are in /etc.
Understand symbolic links
A symbolic link (symlink) is a small file that points to another file or directory. In ls -l it starts with l and shows ->.
ls -l /usr/lib64/libc.so.6
lrwxrwxrwx. 1 root root 12 Oct 2 libc.so.6 -> libc-2.39.so
ls -ld /bin
lrwxrwxrwx. 1 root root 7 Apr 11 /bin -> usr/bin- Programs use the short name (
libc.so.6). An update only changes where the link points. - If the target is deleted, the link is broken (it points to nothing).
Understand hard links
A hard link is a second name for the same data on the disk. The data is only deleted when the last name is removed.
- The number in column 2 of
ls -lcounts the hard links. - Hard links work only inside one filesystem and not for directories.
- How to make both kinds of links is in Copy, move, delete and link files.
Summary of the chapter
ls -l shows details, file shows the type, less reads text, and the FHS tells you where things live.
1Which command shows the details of the directory /etc itself (not what is inside)?
2Where are the settings of the whole system?
In /etc. They are plain text files.
3How do you search for the word "root" inside less?
Type /root and press Enter; n goes to the next match.