ls
: to list directories and filesls -a
: to list all files and directories alongwith hidden onesls -r
: lists files and directories in reverse alphabetical order.ls -l
: to give detailed info of directories/files like when created and by whom, date modified, size, permissions and ownershipsls -al
: to list all hidden files with details.ls -lh
: to list file with details of file size in - KB (kilobytes).ls -lR
: to list all the directories and their - sub-directories with details.ls -lR dirName
: to list the details of - sub-directories of the specified dirName.ls -F
: to identify files and directories - seperately. Directories will be listed with a "/" - symbol at the end of directory namels -i
: displays the inode [index number] of each - filels --sort=t
orls -t
: displays the list of files / directory sorted by time. Similarly sort=v/s/e for sort by version/size/extension
mkdir [FOLDER-NAME]
: to create a folder/ directorymkdir folder1 folder2 folder3
: to create multiple folders/ directory at oncemkdir file{1..9}
: this will create 10 folders with name 'file 1', 'file 2', ..., 'file 10'mkdir -v folders{1..7}
: this will create folders & will skip the ones which are already created before with the same name.mkdir -p [PATH]
: will create 'cmds' folder in the given path but if the path doesn't exits then it will be created.
rmdir [DIR-NAME]
: to remove the file/ folder/ directory.rmdir myFolder*
: removes all folders that begins with name 'myFolder'rmdir *
: removes all the folder in the present directory.rmdir myFolder{1..7}
: removes the folders from 'myFolder 1' to 'myFolder 7' NOTE: rmdir cannot delete diectories having files / folders inside.rm -R [DIR-NAME]
: to delete the directory with the files inside.rm -r [DIR-NAME]
: to delete all the files and NON-EMPTY directories and also the child folders content.
cd
: to change directory to home/user directorycd ~
: to go to home/user directorycd /
: to go to root directorycd [DIR-NAME]
: to change directory or go to that directorycd ..
: to go to the parent directory
touch [FILE-NAME]
: to create a filetouch -m [FILE-NAME]
: change modification time of the filetouch -a [FILE-NAME]
: change the access time of the filetouch test{1..5}.txt
: create 5 files with name test1.txt, test2.txt, ..., test5.txtcat > [FILE-NAME]
: to rewrite a file. If the filename not present then new file with given name will be createdcat >> [FILE-NAME]
: to append new contents to the file [ NOTE: for cat > or >> ... commands, inorder to exit writing mode of the file press CTRL+D.]
rm [FILE-NAME]
: to remove dir/ file/ folder.rm *
: removes all the files in the current directory.rm *.txt
: removes all the files in the current directory that has .txt extension.rm -i [FILENAME]
: to make command prompt the user with each filename while deleting
mv [SOURCE] [DESTINATION]
: to move the file into the given destination or path.cp [FILE-1] [FILE-2]
: copies the file1 contents to file2.cp [FILE-NAME]
: will copy the file in the directory.
cat [FILE-NAME]
: to show the contents of file in the terminals.
mk [PRESENT-FILENAME] [NEW-FILENAME]
: this changes the file or folder name.
od [FILENAME]
: to display a file contents in octal form
- used to display file type
Output:
file video.mp4
video.mp4: ISO Media, MP4 Base Media v1 [ISO 14496-12:2003]
find [FILENAME]
: search for files in a directory hierarchy
- Compare two files byte by byte
Output:
cmp file1.txt file2.txt
file1.txt file2.txt differ: byte 8, line 1
- used to compare files line by line
diff [FILE-1] [FILE-2]
Examples:
- to show the difference of two files side by side
diff -y file1.txt file2.txt
-y
: side-by-side output in two columns
-
Used to compare two sorted files
Contents of file1.txt :
ANGULAR DJANGO ELECTRON FLASK NODEJS REACT YARN
Contents of file2.txt :
ANGULAR JS FASTAPI FLASK NEXT JS NODEJS REACTJS YARN
Command line input:
comm --total file1.txt file2.txt
Output:
ANGULAR ANGULAR JS DJANGO ELECTRON FASTAPI FLASK NEXT JS NODEJS REACT REACTJS YARN 4 4 3 total
- unmatched file of first file is displayed in 1st column
- unmatched file of first file is displayed in 2nd column
- matched lines in 3rd column
--total
: counts the number of lines that are matched and unmatched for both the files.
-
use to change permissions of a file or directory of all types of users
-
Control of a file:
d
: directrory-
: no permission given [NOTE: the leftmost-
implies its a file]- Permissions
r
: read(4)w
: write (2)x
: executable (1)
Example:
chmod +x program.sh
The above command grants executable permissions to the program.sh file for all the users
-rwxrwxr-x 1 deroyace deroyace 127 May 23 15:3 counter.sh drwxr-xr-x 3 deroyace deroyace 4096 Mar 29 19:2 Desktop
rwx
is written 3 times consecutively. This means,- first set of rwx implies USER / OWNER permission
- second set implies GROUP permissions same as owner.
- third set implies OTHERs outside the group.
- left most single symbol
d
indicates it is a directory. It can bel
or-
also.l
means its a shortcut link of a file or a directory.-
means it is a file.
-
Numerical Short hand or Octal Mode
-
Another way to use chmod is to provide the permissions you wish to give to the owner, group, and others as a three-digit number which is basically octets that are formed by combinations of 4,2 and 1.
-
The leftmost digit represents the permissions for the owner.
-
The middle digit represents the permissions for the group members.
-
The rightmost digit represents the permissions for the others.
Value Symbols Meanings/ Permissons 0 --- No permission 1 --x Execute 2 -w- Write 3 -wx Write and execute 4 r-- Read 5 r-x Read and execute 6 rw- Read and write 7 rwx Read, write, and execute
Examples:
- Give read, write, execution permissions to all types of users:
chmod 777 program.sh
- Give read, write, execution permissions only to User and Group:
chmod 770 program.sh
- Give read, write, execution permissions to User and read nad execution permission to Group and only execution permission to other users:
chmod 751 program.sh
- Give read, write, execution permissions to all types of users:
-
Default permissions for:
file
: 664 (-rw-rw-r--)folder
: 775 (drwxrwxr-x)
-
-
Symbolic Mode
Symbol Meaning ------------------- a all users u user g group o others
Examples:
- Give read, write, execute permission to user and execute permission to group and other
chmod u+rwx,go+x program.sh
- Give read permission to all, write permission to user and group, execute persmission to group only:
chmod u+rw,g+rx,o+r program.sh
- Give read, write, execute permission to user and execute permission to group and other:
chmod u+rwx,go+x program.sh
- Make all files and sub-directories in the directory TEST executable by all users
chmod -R a+x TEST
- Remove executable permission from group and other of a file
chmod go-x file.txt
- Give read, write, execute permission to user and execute permission to group and other
chgrp [GROUP-NAME] [FILE/FOLDER NAME]
: to assign a file/folder to a specific group
sudo chown [USER-NAME] [FILE/FOLDER-NAME]
: to change the user or ownership of a file/folder:
- it is used to compress and archive files
- TAR short for Tape Archive
Examples:
-
To create a tar and includes the files specified in the command:
tar cf file.tar [FILE-NAMES]
c
: create new archivef
: to specify name of the archive files[FILE-NAMES]
: represent files that are to be included in the tar file
-
Extract From tar Archive
tar xf file.tar
x
: extract files from an archive
-
Creating an uncompressed tar Archive:
tar cvf file.tar *.sh
- This command creates a tar file called
file.tar
which is the Archive of all.sh
files in the current directory. v
: verbosely list files processed
- This command creates a tar file called
-
Extracting from uncompressed tar archive:
tar xvf file.tar
-
Create a tar.gz Compressed Archive:
tar czf files.tar.gz files
-
Extract files from tar.gz
tar xzf files.tar.gz
-
*
,?
are wilcards -
Use of wild cards:
-
ls *.txt
: list all files that ends with .txt extension*
implies any number of characters
-
To list all files that starts with "file" and succeeds by any one character and then ".txt":
ls file?.txt
-
ls file???.txt
?
two characters???
means any 3 character
-
- to sort lines of text files lexicographically
Examples:
-
sort the lines of file in alphabetical order:
sort [FILENAME]
-
sort the lines of file in reverse order:
sort -r [FILENAME]
-
output the sorted file into a new file:
sort [FILENAME] > [NEW-FILE]
-
output the sorted lines of a file into a new file without duplicates:
sort [FILENAME] > uniq > [NEW-FILE]
-
used to make links between files
-
ln [FILE-NAME] [HARD-LINK]
: Create a hard link for a file (same inode) -
ln -s [FILE-NAME] [SOFT-LINK]
: Create a soft link for a file -
ln -s .. [SOFT-LINK]
: Create a soft link for the parent directory you are currently located in
- prints each line of a file that matches a pattern
Examples:
-
list all directories present in the current folder
ls -l | grep ^d
^d
: matches those lines that starts with letter 'd'
-
Show the line numbers that matches the pattern "hello", ignoring case
grep -i -n "hello" file.txt
-n
: displays line number-i
: ignores case
-
Ignore those lines that matches the pattern "hello"
grep -v "hello" [FILENAME]
-
To highlight words in a file or command output that ends with letter 'A':
grep "\b\w*A\b" [FILENAME]
-b
: boundary\w*
: any number of characters
-
Specifically display and highlight only those words that starts with letter 'A' or 'a':
grep -o -i "\b\A\w*\b" [FILENAME]
-o
: outputs only matching words-i
: ignores case [ NOTE: it works only when capital letter is used in the command to find the required search. ]
- used to extract characters or remove sections from each lines of files
Examples:
-
To extract first three characters of every line:
cut -c -3 [FILENAME]
-
To extract characters starting from 2nd character of every line:
cut -c 2- [FILENAME]
-
To extract last three characters of every line:
cat [FILENAME] | rev | cut -c -3 | rev
-
To display selected columns sorted by ID:
cut -d $'\t' -f1-4 movies.txt | sort -k1 -n
$'\t'
: tab space delimiter [taking columns seperated by TAB space]-d
: delimiter-n
: numeric value sorted-f1-4
: columns 1 to 4-k1
: sort by first column-k2
: sort by second column
paste [FILENAME-1] [FILENAME-2]
: concatenates two files. by default the delimiter is TAB- To explicity specify a delimiter/ concat file contents using delimiter:
paste -d '-' [FILENAME-1] [FILENAME-2]
su root
: to go to the root account. '$' will change to '#'; su -> switch usersudo su
: switching user
-
passwd
: to change your login or root account password. -
passwd [Account Name]
: to change password of that particular account. -
id [USER-NAME]
: shows user id; root account id is 0.
sudo adddgroup [GROUP-NAME]
: to create new User Groupsudo adduser [USERNAME]
: to create a new user account.sudo usermod -aG sudo [USERNAME]
: to make the user as a root user.
sudo groupdel {GROUP-NAME}
: to remove a group:
top
: list all processes and shows the cpu, memory usage and running background apps.ps aux
: lists all processes that are currently running in the device.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.
kill [PID]
: kills or disables a processkill -9 [PID]
: stop a process
-
uname -a
: show kernel information -
xdpyinfo | grep 'dimensions:'
: to check screen resolution Result from above cmd: dimensions: 1366x768 pixels (361x203 millimeters) -
To check distro release version:
lsb_release -a
: provides certain LSB (Linux Standard Base) and distribution-specific information.cat /etc/os-release
-
echo $XDG_CURRENT_DESKTOP
: to check Desktop Environment -
cat /proc/cpuinfo
: cpu information
df
: shows the disk space usagedu
: shows directory space usagefree
: shows memory and swap usagecat /proc/meminfo
: memory information
pwd
: shows the path of current working directorylocate [SEARCH-NAME]
: to search files by namewhereis [APP/COMMAND]
: to locate binary file, manual page files of applications or commandswhich [APP/COMMAND]
: to locate a command or an application
man [COMMAND]
: shows the mannual page of the commandhistory
: list all the cmds used (last 500 cmds)lsusb
: shows the USB buses and devices connectedwhatis [COMMAND]
: to give brief description or use of that commandwhoami
: checks whether you are root user/ whats the user name you are logged in aswho
: display information about users who are currently logged inw
: show who is logged on and what they are doingtty
: print the file name of the terminal connected to standard input!!
: executes the last commandexit
: log out current session
CTRL + ALT + T
: to open terminal [NOTE: this may vary in distros, may not work for some]CTRL + SHIFT + W
orCTRL + D
: to close terminalCTRL + L
: to clear the terminalCTRL + C
: to terminate the programCTRL + D
: TO log out of current session, similar to exitCTRL + Z
: to pause/suspend the programCTRL + A
: to go to start of the line/ commandCTRL + E
: to go to end of the line/ commandCTRL + U
: to clear the commandCTRL + R
: type to bring up a recent command