Santiago Álvarez Rodríguez
Software engineer, front-end developer and language learner.
Everything in Unix is a file...
(kinda)
ls
# On its own, shows contents for current directory
~ $ ls
Desktop Documents Downloads Music Pictures Public Templates Videos
# But...
~ $ ls Documents
Books CV.pdf Forbidden
# Everything in one column
~ $ ls -1
Desktop
Documents
Downloads
Music
Pictures
Public
Templates
Videos
# Show hidden files
~ $ ls -a
. .bash_history .bash_profile .cache Desktop Downloads .local
.. .bash_logout .bashrc .config Documents Music Templates
ls
What about this?
What about this?
ls -l (Long Listing)
# Notice how you can mix options passed to the command (yay!)
~/Documents $ ls -al
total 20
drwxr-xr-x. 2 santiaro90 santiaro90 94 Sep 1 13:52 .
drwx------. 14 santiaro90 santiaro90 4096 Aug 31 09:01 ..
-rw-rw-r--. 1 santiaro90 santiaro90 12 Aug 31 08:11 hello.txt
-rw-rw-r--. 1 santiaro90 santiaro90 3200 Sep 1 14:10 .hidden.txt
-rw-r--r--. 1 santiaro90 santiaro90 320 Aug 26 20:17 ntp.conf
-rw-rw-r--. 1 santiaro90 santiaro90 23 Aug 31 08:16 test.txt
# Ok, I know what . and .. are, but don't wanna see them
total 16
~/Documents $ ls -Al
-rw-rw-r--. 1 santiaro90 santiaro90 12 Aug 31 08:11 hello.txt
-rw-rw-r--. 1 santiaro90 santiaro90 3200 Sep 1 14:10 .hidden.txt
-rw-r--r--. 1 santiaro90 santiaro90 320 Aug 26 20:17 ntp.conf
-rw-rw-r--. 1 santiaro90 santiaro90 23 Aug 31 08:16 test.txt
# Saw that number before the date? That's size in bytes... let's get a nicer format
~/Documents $ ls -Alh
total 16K
-rw-rw-r--. 1 santiaro90 santiaro90 12 Aug 31 08:11 hello.txt
-rw-rw-r--. 1 santiaro90 santiaro90 3.2K Sep 1 14:10 .hidden.txt
-rw-r--r--. 1 santiaro90 santiaro90 320 Aug 26 20:17 ntp.conf
-rw-rw-r--. 1 santiaro90 santiaro90 23 Aug 31 08:16 test.txt
ls -S
; ls -t
(or how to sort ls
output)
# Not necessary, but a good idea to always pass -l option
# Sort by descending file size
~/Documents $ ls -AlhS
total 16K
-rw-rw-r--. 1 santiaro90 santiaro90 3.2K Sep 1 14:10 .hidden.txt
-rw-r--r--. 1 santiaro90 santiaro90 320 Aug 26 20:17 ntp.conf
-rw-rw-r--. 1 santiaro90 santiaro90 23 Aug 31 08:16 test.txt
-rw-rw-r--. 1 santiaro90 santiaro90 12 Aug 31 08:11 hello.txt
# Sort by descending modification time
~/Documents $ ls -Alht
total 16K
-rw-rw-r--. 1 santiaro90 santiaro90 3.2K Sep 1 14:10 .hidden.txt
-rw-rw-r--. 1 santiaro90 santiaro90 23 Aug 31 08:16 test.txt
-rw-rw-r--. 1 santiaro90 santiaro90 12 Aug 31 08:11 hello.txt
-rw-r--r--. 1 santiaro90 santiaro90 320 Aug 26 20:17 ntp.conf
# Revert sort direction
~/Documents $ ls -Alhtr
total 16K
-rw-r--r--. 1 santiaro90 santiaro90 320 Aug 26 20:17 ntp.conf
-rw-rw-r--. 1 santiaro90 santiaro90 12 Aug 31 08:11 hello.txt
-rw-rw-r--. 1 santiaro90 santiaro90 23 Aug 31 08:16 test.txt
-rw-rw-r--. 1 santiaro90 santiaro90 3.2K Sep 1 14:10 .hidden.txt
touch (Create a File)
~/Documents $ touch new_file.txt
~/Documents $ ls -l new_file.txt
-rw-rw-r--. 1 santiaro90 santiaro90 0 Sep 1 15:29 new_file.txt
Yup!
It's that easy!
mkdir (Create a Directory)
~/Documents $ mkdir Important
# -d shows only directory's metadata, instead of its contents'
# -F prints a file-type indicator
~/Documents $ ls -dlF Important
drwxrwxr-x. 2 santiaro90 santiaro90 6 Sep 1 15:37 Important/
mkdir (Create a Directory)
~/Documents $ mkdir -p Important/Docs/Mine/PDF
# -R for recursive
~/Documents $ ls -R Important
Important:
Docs
Important/Docs:
Mine
Important/Docs/Mine:
PDF
Important/Docs/Mine/PDF:
cp (Copy)
~/Documents $ cp hello.txt hello_copy.txt
~/Documents $ ls -l hello.txt hello_copy.txt
-rw-rw-r--. 1 santiaro90 santiaro90 12 Sep 1 16:17 hello_copy.txt
-rw-rw-r--. 1 santiaro90 santiaro90 12 Aug 31 08:11 hello.txt
# Don't let me do something stupid!
~/Documents $ cp -i ntp.conf hello.txt
cp: overwrite ‘hello.txt’? # type 'y' or 'n'...
# WTF???
~/Documents $ cp Important Important_bkp
cp: omitting directory ‘Important’
# Pass -R to recursively copy directories
~/Documents $ cp -R Important Important_bkp
~/Documents $ ls -dl Important*
drwxrwxr-x. 3 santiaro90 santiaro90 17 Sep 1 16:26 Important
drwxrwxr-x. 3 santiaro90 santiaro90 17 Sep 1 16:27 Important_bkp
mv (Move/Rename)
~/Documents $ mv hello.txt hello_copy.txt
~/Documents $ ls -l hello.txt hello_copy.txt
ls: cannot access hello.txt: No such file or directory
-rw-rw-r--. 1 santiaro90 santiaro90 12 Sep 1 16:17 hello_copy.txt
# Again, don't wanna be stupid!
~/Documents $ mv -i ntp.conf hello_copy.txt
mv: overwrite ‘hello_copy.txt’?
# No issues this time... :)
~/Documents $ mv Important Important_bkp
~/Documents $ ls -dl Important*
drwxrwxr-x. 3 santiaro90 santiaro90 17 Sep 1 16:35 Important_bkp
# Put hello_copy in Important_bkp/Docs
~/Documents $ mv hello_copy.txt Important/Docs
~/Documents $ ls -l hello_copy.txt Important/Docs
ls: cannot access hello_copy.txt: No such file or directory
Important_bkp/Docs:
total 4
-rw-rw-r--. 1 santiaro90 santiaro90 12 Sep 1 16:17 hello_copy.txt
drwxrwxr-x. 3 santiaro90 santiaro90 16 Sep 1 16:35 Mine
rm (Remove)
~/Documents $ rm hello_copy.txt
~/Documents $ ls hello_copy.txt
ls: cannot access hello_copy.txt: No such file or directory
# You know what this means...
~/Documents $ rm -i ntp.conf
rm: remove regular file ‘ntp.conf’?
# Not so fast!
~/Documents $ rm Important_bkp
rm: cannot remove ‘Important_bkp’: Is a directory
# Waaat???
~/Documents $ rmdir Important_bkp
rmdir: failed to remove ‘Important_bkp’: Directory not empty
# OK... :)
~/Documents $ rm -r Important_bkp
~/Documents $ ls Important_bkp
ls: cannot access Important_bkp: No such file or directory
Let's have a closer look
at what ls -l
prints
???
This is how Unix describes
file permissions
drwxr-xr-x. 2 santiaro90 santiaro90
-rw-rw-rw-. 1 santiaro90 santiaro90
drwxr-xr-x. 2 santiaro90 santiaro90
-rw-rw-rw-. 1 santiaro90 santiaro90
File type (d: directory, -: regular file, l: link, etc.)
Owner user's permissions*
(r: read, w: write, x: execute)
Owner group's permissions
*A column with - (dash) means that the corresponding permission is disabled
Other users' permissions
Owner user
Owner group
Ok, let's get that straight...
rwx r-x r-- 2 santiaro90 devs
What is this telling us?
santiaro90
.devs
group.santiaro90
can read, write and execute the file.devs
group can read and execute the file, but can't write to it.(We don't care —for now— about that 2)
Each
rwx
triplet can be expressed as a number
Binary | Octal | |
---|---|---|
rwx |
111 | 7 |
rw- |
110 | 6 |
r-x |
101 | 5 |
r-- |
100 | 4 |
See the pattern?
rwx r-x r--
So this....
is the same as...
7 5 4
chmod (Change Mode)
~/Documents $ ls -l hello.txt
-rw-rw-r--. 1 santiaro90 santiaro90 12 Sep 1 16:46 hello.txt
# Add execution permissions for owner user
~/Documents $ chmod u+x
~/Documents $ ls -l hello.txt
-rwxrw-r--. 1 santiaro90 santiaro90 12 Sep 1 16:46 hello.txt
# Remove write permissions for owner group
~/Documents $ chmod g-w hello.txt
~/Documents $ ls -l hello.txt
-rwxr--r--. 1 santiaro90 santiaro90 12 Sep 1 16:46 hello.txt
# Add all permissions for owner group and other users
~/Documents $ chmod go+rwx hello.text
~/Documents $ ls -l hello.txt
-rwxrwxrwx. 1 santiaro90 santiaro90 12 Sep 1 16:46 hello.txt
chmod
(using octal numbers)
~/Documents $ ls -l hello.txt
-rw-rw-r--. 1 santiaro90 santiaro90 12 Sep 1 16:46 hello.txt
# I want all permissions for owner user, read and write
# for owner group, and nothing for other users
~/Documents $ chmod 760 hello.txt
~/Documents $ ls -l hello.txt
-rwxrw----. 1 santiaro90 santiaro90 12 Sep 1 16:46 hello.txt
# Make the file read/write for owner user, read-only for owner group
# and executable for other users
~/Documents $ chmod 641 hello.txt
~/Documents $ ls -l hello.txt
-rw-r----x. 1 santiaro90 santiaro90 12 Sep 1 16:46 hello.txt
# All permissions for owner user, read/execute for owner group
# and other users
~/Documents $ chmod 755 hello.txt
~/Documents $ ls -l hello.txt
-rwxr-xr-x. 1 santiaro90 santiaro90 12 Sep 1 16:46 hello.txt
chown (Change Owner)
~/Documents $ ls -l hello.txt
-rw-rw-r--. 1 santiaro90 santiaro90 12 Sep 1 16:46 hello.txt
# Make 'test' the new owner user.
# You need to use sudo to change user owner.
~/Documents $ sudo chown test hello.txt
~/Documents $ ls -l hello.txt
-rw-rw-r--. 1 test santiaro90 12 Sep 1 16:46 hello.txt
# Assuming 'santiaro90' is the owner user again,
# change the owner group to 'wheel'
~/Documents $ chown :wheel hello.txt
~/Documents $ ls -l hello.txt
-rw-rw-r--. 1 santiaro90 wheel 12 Sep 1 16:46 hello.txt
# Change the owner user to 'test' and the group to 'wheel'
~/Documents $ sudo chown test:wheel hello.txt
~/Documents $ ls -l hello.txt
-rw-rw-r--. 1 test wheel 12 Sep 1 16:46 hello.txt
By Santiago Álvarez Rodríguez
Listing files and directories in Bash, basic file operation commands and changing file permissions.