This is a list of useful Linux commands. Most of them are basic and they can be used as a reference for developers.
files and directories
ls => list all files
ls -a => list all files along with hidden ones
ls -R => list all files and folders recursively
ls -l => list in long format (shows more info)
rm <filename> => deletes a file
mv <filename> <directory path> => moves a file
mv <srcFileName> <destFileName> => renames a file
cp <srcFileName> <destFileName> => copies file
cp -R <srcDir> <destDir> => copies directory
cat filename => read contents of a file
cat > filename => writes to file
touch filename => creates a file, touch is basically used to change file timestamps. Refer here
mkdir directory name => makes a directory
rmdir directory name => removes a directory
rm -rf directory name => removes a directory recursively along with the files
mv dirname1 path or dirname => moves or renames directory
history => list all commands used in the current session
clear => clears the terminal screen
pwd => displays the path of the current working directory.
file permissions
File permissions will be provided to users, groups and others (in the same order). They can be provided as numbers or letters.
r - read, w - write, x - execute u - user, g - group, o - other, a - all
0 - no permission 1 - execute 2 - write 3 - execute + write 4 - read 5 - read + execute 6 - read + write 7 - all
- chmod number filename => gives permissions to user,group and others.
Eg: chmod 777 ex.txt
=> gives all permissions to all users, groups and others.
'+' - gives permission '-' - removes permission '=' - assigns the given permission
Eg: chmod u=rw ex.txt
=> gives read/write to user
Eg: chmod a= ex.txt
=> removes all permission to all users.
- chgrp group name filename => changes group for a file
- chown user filename => changes owner of a file
input/ output directions
'>' write (stdout) Eg: ls -al > filename => writes the output of "ls -al" to the file. If the file already exists, it is overwritten.
'>>' append (stdout) Eg:
echo "hi" >> filename
=> appends to the file'<' input
File descriptors : 0 - stdin, 1 - stdout, 2 - stderr
Error redirection:
This is used to redirect errors to a log file, rather than cluttering the terminal.
Eg: cat ex.txt 2> error.log
=> tries to read the file, if there is any error, it is written to error.log
Here, '2' is the file descriptor for stderr.
working with pipes
pg, more and less
Show large text in the terminal as scrollable chunks by piping it to the pg, more or less commands.
Eg: cat large_file.txt | less
=> gives a scrollable viewer.
grep
Search for a string in the output by piping to grep.
Eg: ls | grep key
=> shows files that have a string "key".
Flags:
- -i => case insensitive
- -c => displays only count
- -n => displays matching line and number
- -v => displays output that didn't match
Eg: ls | grep -cv key
=> displays count to output that didn't match
- grep can also accept regular expressions. Refer here
sort
Sort the contents
Eg: ls | sort
=> displays in alphabetic order
Flags:
- -r => reverse
- -n => numerical
- -f => case insensitive
Eg: ls -l | sort -nk2
=> displays content numerically sorted on the second column. (number of hard links in this case)
variables
- VARIABLE=value => Set variables in the session.
- $VARIABLE => gets the value of a variable.
- unset VARIABLE => unsets the variable.
networking commands
- ping ip => checks the connection
- dig hostname => performs a dns lookup
- ssh -i key username@ip => connects to a remote computer securely. key is the private key file.
- scp -i key.pem source dest => copies a file. remote location should be
host:file
. - lsof -i protocol:port => list all processes listening on a port.
Eg: lsof -i tcp:3000
lists all processes listening to port 3000
process management
- top => lists all running process
- kill pid => kills a process with the given process id
mount device
- lsblk => list all block devices
- mount device id directory => mount a device to a directory
Eg: mount /dev/sda4 /ex/mnt
Flags:
-a => mounts all devices in /etc/fstab
-l => lists all mounted devices
-t => type of filesystem (eg: ext4)
umount device id => unmounts a device
manual
- man command name => shows usage of a command
Thanks for reading :)