Time to study: ~90 min
You will learn: how to move around the tree and how to create, copy, move, delete and read files, plus the * wildcard that makes all of them faster.
← Chapter 2 | back to Terminal 101 | next: Chapter 4 →
1. commands for navigating the file system
previously we wanted to enter the
Terminal-101dir and we explained it in human words. in order to do it in the terminal we usecd(change directory).
# we can use it step by step :
cd /
cd home
cd ati
cd Attia-Pro
cd Projects
cd Terminal-101
# or we can do it in one command :
cd /home/ati/Attia-Pro/Projects/Terminal-101- note: the
/at the beginning means “start from the root”. without it,cd homemeans “thehomefolder inside where i am standing right now”, which is not the same thing.
after we go into the
Terminal-101dir we can see what is inside by usingls(list).
ls01-command-line-basics 02-the-linux-file-system 03-files-and-navigation README.md
let’s say we forgot where we are in the file system, we can use
pwd(print working directory) to find our current path.
pwd/home/ati/Attia-Pro/Projects/Terminal-101
now i found that i am in the wrong dir and i want to go back to the
Projectsdir, so i usecd ...
cd ..
pwd/home/ati/Attia-Pro/Projects
and let’s say i want to go back more than one step, i just repeat it:
cd ../../../
pwd/home
and if i want to go to the root dir there is a too ez command:
cd /
pwd/
i am in the right path right now and i want to create a new directory, i can use
mkdir(make directory).
cd /home/ati/Attia-Pro/Projects/Terminal-101
mkdir practice
ls01-command-line-basics 02-the-linux-file-system 03-files-and-navigation practice README.md
it is created in the current dir, so always check
pwdfirst if u are not sure where u are standing.
let’s say i wrongly created a dir with the name
practisewith the wrong spelling and i want to remove it, i can usermdir(remove directory).
mkdir practise
rmdir practise
ls01-command-line-basics 02-the-linux-file-system 03-files-and-navigation practice README.md
- note:
rmdironly works on an empty directory. if there is anything inside it, it will refuse:
rmdir practicermdir: failed to remove 'practice': Directory not empty
for that case we have
rm -rfwhich is at the end of this section.
now i want to create a new markdown file with the name
notes, i can usetouch.
cd practice
touch notes.md
lsnotes.md
now i want to remove the file i created since i made it in the wrong dir, i use
rm(remove).
rm notes.md
ls(nothing, the dir is empty now)
also there is a command which i mostly use to remove a directory:
rm -rf. the-rmeans recursive and the-fmeans force, so it will remove the dir and whatever is inside it.
-
this command is a very powerful one, because sometimes we have folders which contain corrupted data or files that do not want to be deleted, and with this command u can do it like a piece of cake.
-
careful: it never asks u to confirm and there is no trash bin to get it back from. always run
pwdandlsbefore u run it, and never run it on/.
cd ..
mkdir -p old-stuff/inside
touch old-stuff/inside/file.txt
rm -rf old-stuff
ls01-command-line-basics 02-the-linux-file-system 03-files-and-navigation practice README.md
now i want to copy a file into one of the dirs, we can use
cp(copy).
cp README.md practice/
ls practiceREADME.md
u can also copy a whole directory with the
-Rflag, which means copy the directory and its content recursively.
cp -R practice practice-backup
ls01-command-line-basics 02-the-linux-file-system 03-files-and-navigation practice practice-backup README.md
oops, i put the file in the wrong dir, i want to move it to another dir, i can use
mv(move).
mv practice/README.md practice-backup/
ls practice-backupREADME.md
i added some text inside my file and i want to see it without opening a GUI editor, i can use
cat.
cd practice-backup
cat README.md# Terminal 101
a course about the terminal, written while i was learning it.
now we have another command which is similar to
cat, which isless.
less README.md# Terminal 101
a course about the terminal, written while i was learning it.
:
- note: the difference between
catandlessis thatcatconcatenates the file and prints it all at once in the terminal, whilelessopens an interactive viewer which enables u to scroll up and down to see the content.lessis preferred for larger files. - to get out of
lesspressq.
now i want to print the first 10 lines of the file or the last 10 lines, i can use
headandtail.
head README.md# Terminal 101
a course about the terminal, written while i was learning it.
## Chapters
1. Command Line Basics
2. The File System
3. Search
4. Permissions
tail README.md5. Scripts and PATH
6. Input, Output and Processes
7. Text Editors
## How to study
read the chapter, then do the assignment at the end of it.
before u move to the next chapter, make sure u can do the
assignment without looking at the solution.
we can also add the
-nflag which means how many lines will be printed.
head -n 3 README.md# Terminal 101
a course about the terminal, written while i was learning it.
tail -n 3 README.mdread the chapter, then do the assignment at the end of it.
before u move to the next chapter, make sure u can do the
assignment without looking at the solution.
2. the * wildcard
everything above works on one file at a time. but most of the time u want to do the same thing to 20 files, and typing 20 names is not realistic.
so we have the wildcards.
*means “anything, any number of characters”.
let’s say this is what we have:
lsnotes.md notes.txt old.txt photo.jpg readme.md
ls *.txtnotes.txt old.txt
ls *.mdnotes.md readme.md
and it works with every command we learned above, not only
ls:
cp *.txt backup/ # copy every txt file into backup
rm *.jpg # delete every jpg file
mv *.md notes/ # move every md file into notesnow the part that matters, and it is the thing that confuses everyone including me:
the
*is not a feature oflsorcporrm. it is the shell.
before
lseven starts, the shell looks at*.txt, finds the files that match, and replaces the*.txtwith their names. solsnever sees a star at all. it just receives a list of file names as if u typed them by hand.
u don’t have to believe me, u can see it happen.
echojust prints what it is given, so let’s give it a star:
echo *.txtnotes.txt old.txt
echohas nothing to do with files. it printed the file names because the shell had already replaced the*.txtbeforeechoran. that is the whole idea, and once u see it once u never forget it.
- careful: this is also why
rm *is so dangerous. u are not askingrmto be clever, the shell hands it every single file name in the folder andrmdeletes all of them, exactly as told. always runls *first to see what u are about to hit, then changelstorm.
the other wildcards, u will use them less but u should recognize them:
?matches exactly one character:
ls file?.txtfile1.txt file2.txt
so
file1.txtandfile2.txtmatch, butfile10.txtdoes not, because that is 2 characters where the?allows one.
[...]matches one character out of a list. so[12]means “either a 1 or a 2”, not “12”:
ls file[12].txtfile1.txt file2.txt
and now, when we get to
findin Chapter 4 and it tells u to put ur pattern in quotes, u will know exactly why. the quotes stop the shell from eating the star, so thatfindgets to see it and use it itself.
NOTES
man: it is used to show the manual of a command.
since we have learned the most important basic commands in the terminal, i want u to know also a very useful command which is
man.
-
let’s say u learned all the commands above or other ones, doesn’t matter, but u did not use them for a long time so u are not able to remember all of them, which is expected. so u can use the
mancommand which shows u the manual of a command. -
example: u have this powerful and dangerous command
rm -rfbut u can not remember what the-ror the-fare, so u can check themanpage of it.
man rmRM(1) User Commands RM(1)
NAME
rm - remove files or directories
SYNOPSIS
rm [OPTION]... [FILE]...
DESCRIPTION
This manual page documents the GNU version of rm. rm removes
each specified file. By default, it does not remove directories.
OPTIONS
-f, --force
ignore nonexistent files and arguments, never prompt
-r, -R, --recursive
remove directories and their contents recursively
...
by reading this manual u can basically know what is what and remind urself of the commands. press
qto get out of it, same asless.
- the
flags:
- u saw them already:
ls -l,ls -a,ls -la. what comes after the-is the flag, and it may be a small or a big letter, they are not the same thing. - most commands have a short version and a long version of the same flag.
ls -ais the short one andls --allis the long one, they do exactly the same. the long one is easier to read in a script, the short one is faster to type. - u can stack short flags together,
ls -lais the same asls -l -a. - if u check the
manpage u will find both versions listed next to each other for every command.
- the path shortcuts:
these are not commands, they are shortcuts u can use anywhere u would write a path.
~means ur home dir:
cd ~
pwd/home/ati
.means the current dir. u may already have seen it used by most developers when they write something likecode .which basically means open vscode with the current dir...means the parent dir, one step back. that is whycd ..works.
in Chapter 7 we will learn how to make ur own shortcuts with
alias, and how to keep them forever in the.bashrcfile.
- the positional arguments matter:
which means which argument u write first and which one u write last changes what happens.
mv notes.md ../
# or
mv file1.md file2.md ../so we start with the file names and the directory name goes last.
- small but important note:
mv file1.md file2.mdif we use
mvwith only two file names and no directory at the end, we are taking the content offile1.mdand putting it infile2.mdand removingfile1.md. basically it is renamingfile1.mdto befile2.md.
Assignment
- Go to ur home dir using the shortcut, then print where u are to prove it.
- Create a dir called
ch3-practiceand go inside it. - Create 3 files:
one.txt,two.txt,three.txt. - Write ur name inside
one.txtand read it back without opening an editor. - Rename
two.txttorenamed.txt. - Make a dir called
backupand copy all 3 files into it using a wildcard, not by typing the 3 names. - Print only the first 2 lines of
one.txt. - Use
echoto prove to urself what*.txtexpands to before the command runs. - Create
file1.txt,file2.txtandfile10.txt, then list only the two with a single digit. - Delete the whole
ch3-practicedir in one command.
stuck, or done and want to check? the solutions are here