Bash & Shell Flashcards
Unix can be divided in … parts. What are they?
3 parts:
- Kernel
- Shell
- Programs
What happens when you type the command “rm file.txt”?
Shell is going to search for the program “rm”, check if the syntax is correct and send a ‘msg’ to kernel that could be translated to “execute program rm on file.txt”. Kernel is then going to allocate time and memory for the process and will execute it.
We can say everything in UNIX is a …. or a …..
A process or a file
Command to list content of present directory with common flags.
ls
flags:
- a: includes hidden content
- l: shows info of content
- h: format sizes to ‘human-readable’ when using with l
Command to create a directory
mkdir dir_name
Command to change directories with common usage
cd
some uses:
cd .. (goes back one)
cd ~ or just cd: goes to home directory
cd path: goes to path
cd - : goes to previous directory
Command to show full path
pwd
Command to copy a file
cp file1 file2
Commando to rename (or move) file
rename: mv file1 file2
move: mv oldPath newPath
Command to remove file
rm file
flags:
- r: do it recursively (on a dir)
- f: force removal (used so we’re not asked for every file inside the dir we want to remove)
Command to remove an empty directory
rmdir
Command(s) to show contents of a file.
cat file: prints the entire file on screen
less file: allows for a page-view of file content
Commands to show the first or last few lines of file
first: HEAD -n
last: TAIL -n
Where n is the number of lines. Default is 10
Command to search a file for key-words
grep ‘keyword’ file
flags:
-v: return non-matching lines
Command to count lines/words/chars of a file
wc file
flags:
- l: lines
- w: words
- c: chars
How to redirect an output of a command to a file?
Command > file
What’s the problem of redirecting output ( > ) to a file?
When there’s a ( > ) on the entire command, the file receiving the output is TRUNCATED, that is, has all of it’s content erased, becoming a 0byte file.
What’s redirected to a file when using the ‘short syntax’ command > file and how to correctly use it?
When not specified, the redirection only handles the STDOUT (1) to the file. If an error happens, the STDERR (2) is not redirected, and thus is displayed on screen..
To fix that, we do:
command 1> file
or
command2>file
Where’s the common place to redirect output we want ‘gone’?
/dev/null
What does the ‘touch file’ command do
If the file doesnt exist, it creates it.
If the file exists, it just updates it’s “last modified” time
How to append the output of a command to a file?
command»_space; file
How to create a file with some content already in it?
cat > file
And then start to type the content, hitting enter to jump lines and ctrl+d to exit and save it.
How to append some text to a file?
cat»_space; file
And then start to type the content, hitting enter to jump lines and ctrl+d to exit and save it.
Command to concatenate files?
cat file1 file2 > file3
Warning: be aware of how a file is truncated when using the ( > )