100 Basic Linux Commands đ
January 30, 2021â˘1,227 words
Execute the previous command used:
!!
Execute a previous command starting with a specific letter. Example:
!s
Short way to copy or backup a file before you edit it. For example, copy nginx.conf
cp nginx.conf{,.bak}
Toggle between current directory and last directory
cd -
Move to parent (higher level) directory. Note the space!
cd ..
Go to home directory
cd ~
Go to home directory
cd $HOME
Go to home directory (when used alone)
cd
Set permissions to 755. Corresponds to these permissions: (-rwx-r-x-r-x), arranged in this sequence: (owner-group-other)
chmod 755 <filename>
Add execute permission to all users.
chmod a+x <filename>
Changes ownership of a file or directory to .
chown <username>
Make a backup copy of a file (named file.backup)
cp <file> <file>.backup
Copy file1, use it to create file2
cp <file1> <file2>
Copy directory1 and all its contents (recursively) into directory2
cp -r <directory1> <directory2>/
Display date
date
Zero the sdb drive. You may want to use GParted to format the drive afterward. (Requires sudo).
dd if=/dev/zero of=/dev/sdb
Display disk space usage
df -h
Take detailed messages from OS and input to text file
dmesg>dmesg.txt
Display a LOT of system information. I usually pipe output to less. You need elevated permissions to run this (sudo).
dmidecode
Display BIOS information. You need elevated permissions to run this (sudo).
dmidecode -t 0
Display CPU information. You need elevated permissions to run this (sudo).
dmidecode -t 4
Search for installed packages related to Apache
dpkg âget-selections | grep apache
Shows you where in the filesystem the package components were installed
dpkg -L <package_name>
Display detailed disk use for each subdirectory
du / -bh | less
Print the environment variable PATH
echo $PATH
Display environment variables like USER, LANG, SHELL, PATH, TERM, etc.
env
Opens a picture with the Eye of Gnome Image Viewer
eog <picture_name>
Quit the terminal (or give up super-powers if youâve previously done sudo su)
exit
Display memory usage
free
Easy way to view all the system logs.
gnome-system-log
Search through file(s) and display lines containing matching string
grep <string> <filename>
Get the number of seconds since the OS was started
grep btime /proc/stat | grep -Eo "[[:digit:]]+"
Display the last 1000 commands
history | less
Display the name of the local host
hostname
Display time.
hwclock âshow
Display user id (uid) and group id (gid)
id
Display your local IP address and netmask
ifconfig
Wireless network interface
iwconfig
Display wireless network information
iwlist
Kill process by name. You need elevated permissions to run this (sudo).
killall process
Get the date and time of the last system shutdown
last -x | grep shutdown | head -1 | grep -Eo "[A-Z][a-z]{2} [[:digit:] ][[:digit:]][[:digit:]]{2}:[[:digit:]]{2}"
Quit shell session (only for a shell youâve logged into like one of the virtual consoles)
logout
List non-hidden files and subfolders in current directory (like dir for windows). Use -R for recursive and -a to include hidden files.
ls
Display file access permissions for all files in the current directory. The format for permissions is drwxrwxrwx where the order is owner-group-other and the numeric values are read=4, write=2, execute=1.
ls -l <filename>
List all available applications, in case youâve forgotten how to open Open Office Writer or another application from the terminal (oowriter)
ls /usr/bin | less
Display more networking information
lshw -C network
Display kernel modules currently loaded
lsmod
Display sound, video, and networking hardware
lspci -nv | less
Display usb-connected hardware
lsusb
Read the commandâs man page (manual)
man <command>
Create new directory at specified location
mkdir <dirname>
Move file to specified directory
mv <file> <dir>
Rename file1 to file2
mv <file1> <file2>
Display routing table
netstat -rn
Print environmental variables
printenv
List the processes currently running by this user. There are many useful options, view them with ps âhelp
ps -Af
Print working directory
pwd
Delete file
rm <filename>
Delete directory and all itâs contents
rm -rf <dir>
Removes all files that end in txt in current directory
rm *.txt
Delete directory (will only work if itâs empty)
rmdir <dir>
Display your default gateway listed under âdefaultâ
route
Completely destroy all traces of the file. This takes a while. -n 7 means seven overwrites, -z means zero the bits afterward to hide shredding, -u means delete the file when done, and -v means verbose.
shred -zuv -n 7 <file>
Shutdown now.
shutdown -h now
Restart now.
shutdown -r now
Log into remote computer
ssh <IP address>
Open the root shell, giving yourself superuser permissions until you relegate your powers with exit. Unlike sudo su which does the same thing, this method of starting the root shell is uncorrupted by a userâs environmental variables.
sudo -i
Open the root shell, like sudo -i, but this method retains the userâs environmental variables. Relegate superuser permissions and return to normal shell with exit.
sudo su
Creates a compressed archive of the specified directory and all files/directories under it.
tar czf <dirname>.tgz <dirname>
Expand the contents of a compressed archive and extract to current directory.
tar zxvf <archive>
List current processes by cpu use. This is very useful. Press q to quit and h for help.
top
Create an empty file if it doesnât exist
touch <filename>
Display the name of the current terminal
tty
Display your linux kernel
uname -a
Display your machineâs processor architecture
uname -m
Returns one-line synopsis from the commandâs man page
whatis <command>
Returns the location of the program in the filesystem
whereis <command>
Returns the applicationâs path
which <command>
Display the users logged into the machine
who
Display your login name
whoami
This will display the output of test.log as it is being written to by another program
tail âfollow test.log
If youâve just navigated to a directory shell and want to open a file or application IN that directory. Just use this command followed by the filename
./filename.txt
Escape operator. Use it before a space if youâre trying to open a file that has whitespace in the name.
\
The tilde represents your home directory.
~
Run any command when the system load is low
batch <command>
Display cpu info
cat /proc/cpuinfo
Display memory usage
cat /proc/meminfo
Display networking devices
cat /proc/net/dev
Display performance information
cat /proc/uptime
Display kernel version
cat /proc/version
Display file contents
cat <filename>
List partition tables
fdisk -l
Show the properties/compression of a file or package
file <package_name>
Find a file. Search Linux filesystem for a file name.
find / -name <filename>
To create a *.gz compressed file
gzip test.txt
To uncompress a *.gz file
gzip -d test.txt.gz
Display compression ratio of the compressed file using gzip -l
gzip -l *.gz
Output file status
stat filename.txt
Download a file from the internet
wget http://remote_file_url
Show list of last 10 logged in users.
last -n 10
Display a tree of processes
pstree