Basics linux commands
sudo - Stands for Super User Do. The sudo command allows you to run commands with the superuser privileges.
whoami - Displays currently logged-in user.
cat - File concatenation and printing are done using the cat command.
cat <file-name>
- displays the contents of the file.cat > <file-name>
- creates a new file, allows to input content interactively and redirects inputted content to the created file (> redirection operator).echo - displaying lines of text or string which are passed as arguments on the command line. Can be used to create a file or empty a file.
echo "Hello"
- prints Helloecho "Hello" > hello.txt
- Creates a file hello.txt with Hello as content in it.echo "World" >> hello.txt
- appends the content(World) to the file hello.txtecho > hello.txt
- Empties the content from hello.txt file.pwd - shows the present working directory (abbr. Print Working Directory).
mkdir - make directory.
mkdir <dir>
- creates the directory.mkdir -p /A/B/C/D
- creates directories (/A/B/C/D) with its parent directories if it does not exists (-p parent).ls - list files or directories
ls -a
- list all hidden files and directoriesls -altrh
- lists all files & folders along with hidden files in a formatted manner-a all
-l long listing format
-t sort by time, newest first
-r reverse order while sorting
-h --human-readable with -l and -s, print sizes like 1K 234M 2G etc.
touch - creates an empty file or updates timestamp of the existing file.
touch <file-name>
- creates a single empty file named <file-name>.touch -a <file-name>
- change only the access timetouch -m <file-name>
- change only the modification timestat - Display file or file system status. It will display file size and timestamp (access, modify, and change time)
head <file-name> - displays first 10 lines of the file by default.
head -n 5 <file-name>
- displays first 5 lines of the file (-n number).head -n -5 <file-name>
- print all but the last 5 lines of the file.tail <file-name> - displays last 10 lines of the file by default.
tail -n 5 <file-name>
OR tail -n -5 <file-name> - displays last 5 lines of the file (-n number).tail -n +5 <file-name>
- print all lines starting from the line 5.sort - The sort command sorts the contents of a file, in numeric or alphabetic order, and prints the results to standard output.
-h, --human-numeric-sort compare human readable numbers (e.g., 2K 1G)
-n, --numeric-sort compare according to string numerical value
-r, --reverse reverse the result of comparisons
rm - remove command.
rm <file-name>
- removes the file.rm -d <dir-name>
- removes empty directory .rm -r <dir-name>
- removes files & directories recursively from a directory (-r, -R, --recursive remove directories and their contents recursively).rm -rf - force remove the files & directories recursively from a directory (-f force).
cp - copy command.
cp <src-file/dir> <dest-file/dir>
- copy the files and directories from source to destination.cp -rp <dir1> <dir2>
- copy dir1 directory to dir2 directory recursively-r recursive
-p same as --preserve=mode,ownership,timestamps
mv - move or rename command.
mv <old-filename> <new-filename>
- renames the file to new name.mv <filename> <dirname>
- moves the file to the directory.