date
: current datecal
: current months calendarcal 1999
: calendar for 1999cal -3
: calendar (last month, current and next month)cal -y
: all year calendarclear
: clear console textexit
: exits de terminal
- bin: binary (commands and utilities that all users can run)
- sbin: this directory contains programs that performs vital system tasks (network management, disk partitioning). Only the superuser has access to these programs.
- home: each user is given a directory under the home directory. A user can store anything in his home directory
- opt: optional (additional software)
- tmp: temporary files, files created by various programs (Generally cleared on reboot)
- var: variable data, data that frequently changes over time.
- Log files
- Data bases
- User mail
- Spools -> temporary storage location
- /var/spool is traditionally used for machine-local data being spooled to or from UNIX subsystems. For example, print jobs are spooled here for delivery to the lineprinter daemon, out-bound mail is spooled for delivery to remote systems, and UUCP files are spooled for transmission to UUCP neighbors. In-bound mail and news are spooled here for delivery to users, and at and cron jobs are spooled for delayed execution by the cron daemon.
-
pwd
: print working directory -
ls
: list directory -
cd
: change directory -
cd /
: take you to the root directory -
cd ~
: take you to the user home directory -
cd ./another_folder
: navigate from current directory to another_folder -
cd ..
: navigate from current directory to parent directory -
cd -
: go back to the previous working directorycd /
: going to the root directorycd ~/
: going to the home directorycd -
: going back to the root directory
-
cd "my work"
orcd 'my work'
orcd my\ work
: going to a folder with space -
ls /
: display your root directory -
ls ~
: display your home directory -
ls ..
: display the content of my parent directory -
[DOES NOT WORK]
ls -
: should display you the content of your previous working directory
- Every file in the system has an inode (Index node)
- Contains all file information except the file content and name
- A database of a file
- They contain:
- Inode number
- File size
- Owner information
- Permission
- File type
- Number of links
- Soft Link
- Hard Link
- etc...
- A soft link is the same as a shortcut in Windows
- Aka Symbolic link
- It is a pointer to the original file
- It is a file pointing to another file
- Different inode number
- Small file size
- In case the original file gets deleted, the soft link will no longer work
- You can create soft links for directories
- Different name of the same file
- Same file size
- Same inode number
- You cannot differentiate between a real file and a hard link
- They are kind of a copy of the file
- If I change a hard link file content, it will reflect on the other files (the original and other hard links)
- In case I delete the original file, the hard link file will still work
- Be careful: You should not create hard links for directories. Normally they are not even allowed because they break the file system structure.
-
ls -i
: show inode id of files -
ls -l
: show files information (size in bytes, permission, etc.) -
ln
: link -
ln file_name hard_link1
: Create a hard link for a file (same inode) -
ln -s file_name soft_link1
: Create a soft link for a file -
ln -s .. c
: Create a soft link C for the parent directory you are currently located in
-
ls
: List files by alphabetic order -
ls -i
: This will list the index node number of each file -
ls -a
: This will show all the files (-a == all files) in your current directory, including hidden files -
ls -l
: Long listing of all files in the directory and some important information. -
ls -t
: Sort files by modification date -
ls -r
: List the files in reverse fashion (in this case reverse based on alphabetic order) -
ls -rt OR ls -r -t
: List the file in reverse fashion (in this case reverse based on modification date) -
ls -li
: Long listing + show inode ids -
ls -lia
: Long listing + show inode ids + hidden files -
ls -R
: Show current directory content plus any children/sub directory content as well -
ls -Ra
: Show current directory content plus any children/sub directory content as well + including hidden files
-
Touch is used to create empty files
touch file_name
: Create a file called file_nametouch file_name1 file_name2 file_name3
: Creates 3 filestouch 'my file'
ortouch "my file"
ortouch my\ \ file
: create a file with a name containing spaces\
is a scape sequence
-
Touch is also used to update a current files timestamp (modification dade)
touch newFile
echo "test" > newFile
touch newFile
: this will update the file timestamp and keep the content
-
If you want to create a directory, then you use
mkdir
command as follow:mkdir directory_name mkdir directory_name1 directory_name2 directory_name3
-
To remove empty directories you can use
rmdir
command as follows:mkdir empty_dir rmdir empty_dir
- In case you do
rmdir
in one directory that has files and another that is empty, it will only delete the empty one.
- In case you do
-
To create a directory with a name containing spaces, you can do
mkdir 'my directory'
ormkdir "my directory"
-
To delete non-empty-directory files, or just normal files you just do:
touch fileToDelete rm fileToDelete mkdir folder cd folder touch file cd ..
-
rm -R folder
orrm -r folder
-
In case of non-empty-directory you need to run
rm
with the recursive flag-R
. Otherwise it will fail -
Also, the
-R
flag can be lowercase since it is not case sensitive forrm
(although it is case sensitive forls
)
-
-
rm
options:-
rm -i
: Prompt you before removing any existing file. the-i
means interactive mode -
rm -f
: Never prompt you before removing a file. And will not display a warning if the file you are trying to delete does not exist, meaning that it will ignore non existent files. -f means force -
rm -v
: Verbose mode. It will print the name of each file before removing it. -
rm -R
orrm -r
: Recursively delete files. If the file is a directory, remove the entire directory and all its contents, including subdirectories.
-
-
The
cp
command copies files or directories. It can be used in two different ways:-
Copy a file:
cp file file2
-
Make a copy of a directory:
cp -R dir dir2
:dir2
does not need to exist- This will make a copy of dir1 named dir2 (Assuming that dir2 didn't exist)
-
In case you copy many files using the following syntax:
-
cp file1 file2 file3 dir
:dir
must exist in this case. -
Same case for directories:
cp -R dir1 dir2 dir3
-
-
cp
options:cp -i
: Before overwriting an existing file, prompt the user for confirmation. cp will silently overwrite files by default.cp -v
: Verbose mode (print the name of each file that was copied).cp -R
: Recursively copy directories and their content. Just like therm
command, this option must be specified when copying a directory.cp -r
: Same as cp -R
-
The
mv
command can be used in two different ways.-
Renaming files
mv file1 file2
: This will rename file1 to file2mv dir1 dir2
: If your file is a directory, this will rename the directory- You do not need to use the -R option with
mv
-
Moving files
mv file1 file2 dir1
: This will move file1 and file2 to dir1. However, dir1 must existmv dir1 dir2 dir3
: This will move dir1 and dir2 to dir3. Again, dir3 must exist
-
-
mv
options:-
mv -i
: Before overwriting an existing file, prompt the user for confirmation. If the option is not specifiedmv
will silently overwrite the file -
mv -v
: Verbose mode (print the name of each file that was moved or renamed)
-
-
Filenames are case sensitive in just like the commands are.
-
Linux has no concept of
file extension
like Windows. You can have files without any extension.- It checks the file content to identify what kind is it.
-
The command
file filename
will give us information about the file type. -
file LS -L OUTPUT.png
: LS -L OUTPUT.png: PNG image data, 1968 x 488, 8-bit/color RGBA, non-interlaced
history
: Show all the commands executed in the cmdhistory 10
: Show last 10 commandshistory -c
: Clear history- Your history is stored at
~/.bash_history
- Your history is stored at
!number
: Runs a command from history
Control+a
: Start of the lineControl+e
: End of the lineControl+d
: Remove character from left to rightControl+l
: Clear
-
less file1
: View the content of the filefile1
- Press
q
to quit
- Press
-
cat file1 file2
: View the content of the filefile1
concatenated withfile2
cat
lets you see two files output concatenated
-
tac file
: Shows the content of file, but reversed -
head -n 20 file
: Show first 20 lines of file -
tail -n 20 file
: Show last 20 lines of file
wc file
: Presents the data like -> Number of lines, number of words, number of byteswc -l
: Show just number of lineswc -w
: Show just number of wordswc -c
: Show just number of byteswc -L
: Length of longest line in characters
-
bash
: Born again shell -
Executable programs
- They are inside
/bin
andusr/bin
. - Example:
cp
command
- They are inside
-
Shell builtin
-
cd
command -
In computing, a shell builtin is a command or a function, called from a shell, that is executed directly in the shell itself, instead of an external executable program which the shell would load and execute.
Shell builtins work significantly faster than external programs, because there is no program loading overhead. However, their code is inherently present in the shell, and thus modifying or updating them requires modifications to the shell. Therefore, shell builtins are usually used for simple, almost trivial, functions, such as text output.
-
-
Shell scripts
- Custom programs written in shell script
-
Alias
ls
command- Create your own commands
- The alias command makes it possible to launch any command or group of commands (inclusive of any options, arguments and redirection) by entering a pre-set string (i.e., sequence of characters).
-
Display information about command type:
- It displays if command is an alias, shell function, shell builtin, disk file, or shell reserved word.
type ls
: ls is hashed (/bin/ls)type cd
: cd is a shell builtintype cp
: cp is /bin/cpfile /bin/cp
: tells you thatcp
is an executable
-
which
will display the path of shell scripts and executables, it does not support shell builtins.which cp
: display executable location/bin/cp
-
For shell builtin help you can do
help cd
-
For executable help you can do
man cp
- Also for executables, you can do
whatis cp
to get a short description about the program
- Also for executables, you can do
date; cal
: Separate commands using a semicolonmkdir test && cd test1 && cd test
: Different from;
the operand&&
stops the execution if one of the commands fail (short circuit evaluation)
-
*
: all occurrencescp *.txt dir
cp * dir
-
*.png
returns all the files that end with the extension .png -
pea?.png
returns all files that start with "pea", is followed by any single character, and end with .png (for example, peas.png and pear.png) -
*.{txt,pdf}
returns all files that end with either .txt or .pdf -
log[12345].pdf
returns files log1.pdf, log2.pdf, log3.pdf, log4.pdf and log5.pdf -
?
: represents a single character -
cp file?.txt dir
: copy all files file0.txt, file1.txt, etc. Any file that has a name structure likefile
+ an unknown character +.txt
-
[]
: range of characters-
cp [abc]*.txt dir
: Copy all the file that begins with the letters abc todir
-
cp [!abc]*.txt dir
: Copy all the file that does not begins with the letters abc todir
- Exclamation mark negates the command
-
cp [0-9]*.txt dir
: Copy all files starting from 0 to 9 -
cp [[:upper:]]* dir
: Copy all the files that starts with an upper case letter -
cp [[:lower:]]* dir
: Copy all the files that starts with an lower case letter -
cp *[[:digit:]] dir
: Copy all the files that ends with a digit -
cp [[:alpha:]] dir
: Copy all the files that starts with a character from the alphabet -
cp [[:alnum:]] dir
: Copy all the files that starts with a character from the alphabet or a number. (alpha numeric) -
cp [[:alnum:]][0-9]* dir
: Copy all the files that starts with a character or a number (alpha numeric) and the second character must be a number from 0 to 9 -
cat ???
: View all files with exactly 3 characters -
rm [[:digit]]*[abc]
: Remove all the files that begins with a number and ends with the letter (a,b or c) -
rm [[:digit]]*abc
: Remove all the files that begins with a number and ends with the letters abc all together
-
-
alias myname="cd Desktop;mkdir dir"
: Create an alias called myname that goes to the Desktop folder and creates a directory called dir -
unalias myname
: Delete an alias -
To retain an alias, save it inside
~/.bashrc
-
alias
: List of all alias in the system
-
>
operator- e.g.
grep "text" . > output.txt
- Writing to a file (overwrite)
- e.g
head -n1 < /etc/passwd
- Using operator as input to an app
- e.g.
-
>>
operator- e.g.
grep "text" . >> output.txt
- Append to a file
- e.g.
-
ls | cowsay
- pipe the results from
ls
to the programcowsay
- pipe the results from
-
ls -l *.conf | wc -l | cowsay
-
ps aux
a
=--all
Display information about other users' processes as well as your own.u
= Display the processes belonging to the specified usernames.x
= When displaying processes matched by other options, include processes which do not have a controlling terminal.
-
top
-
htop
-
adduser danielmapar
- Only root may add a user or group to the system.
sudo
:super user do
sudo -i
: login asroot
-
sudo adduser danielmapar
- Executing command with root credentials
-
sudo login danielmapar
cat /etc/passwd | grep "^danielmapar"
-
- user = file owner
- group = the group of the file owner
- others = other users / groups
- The first char can be
-
(for file),d
for directory ofl
for link.
-
chmod +x file
- You can change the
user
andgroup
permission to includex
(execute).
- You can change the
-
chmod 755 file
-
each number represents a sequence of 3 bits. In the case of 7 it represents 111 (read, write and execute). Another example is 5 (read, dont write, execute).
- In this case, 7 changes the
user
permissions (rwx
), the second and third numbers (5's) will change thegroup
andother
permissions respectively.
- In this case, 7 changes the
-
Anothe way to achieve this (
755
) is by running:chmod u=rwx,g=rw,o=rw file
-
-
echo $USER
-
echo $HOME
-
echo $0
- my shell name
-
echo $PATH
- Path to find binaries
PATH=$PATH:newfolder
-
env
: list of env vars -
RC
stands for run commands..bashrc
= Bash Run Commands.
-
export $PATH=$PATH:/newpath
- the keyword
export
guarantees that if our current process spins child processes, it will also export thisenv
var to the children processes.
- the keyword
-
- Variables in
sh
are always strings. We useexpr
to evaluate numerical expressions. - We escape
*
so it does not get confused by shell with a wildcard.$((1 + 1))
also works forbash
only
- Variables in
-
$#
represents the number of parameters being supplied to the script. -
` backticks are used to execute an expression.
-
- Space inside the brackets are mandatory.
-
==
is specific tobash
(not present in sh (Bourne shell), ...). UsingPOSIX
=
is preferred for compatibility. In bash the two are equivalent, and in sh = is the only one that will work.
-
^
means it should start with string.
-
You can detache a process from the terminal by adding
&
at the end../my-script.sh &