A-Z Popular Blog Encyclopedia Search »
Technology
 Advertisements
Information Technology

Integration

It Management

Systems Technology

Information Security

Technology Management

Networks

Technology Culture

Artificial Intelligence

Low Technology

Computing

Coding

Space

Automation

63 Useful Linux Commands

 ,
Linux commands are instructions that can be issued to the linux operating system from a command line or script. The following are useful commands that are enough to competently use a linux box and perform basic administration functions.
Command
Common Usage

su

su someuser
Switch user to someuser. This will prompt you for someuser's password.
su
Switch user to root.

sudo

sudo ls
Execute the command ls with root permissions.

alias

alias ls='ls -la'
Substitute one string for another when executing commands. Used to create custom commands and modify the behavior of existing commands.
alias
Shows a list of active aliases. If a command is behaving strangely, it may be aliased.

unalias

unalias ls
Removes an alias associated with the string "ls."

clear

clear
Clear the screen of a terminal.
ls
List files in the current working directory.
ls /tmp
List files in the directory /tmp.
ls -l
Show more details such as file permissions.
ls -t
Sort files by modification date in descending order with newest files first.
ls -tr
Sort files by modification date in ascending order with newest files last. Often more useful as you don't need to scroll up to see the newest files.
ls -a
Show hidden files.
ls -altr
A commonly used combination of command parameters for ls that lists a detailed view of all files in ascending order by modification date.

wc

ls -1 | wc -l
wc stands for word count. It is a tool for counting symbols in text. The command above lists the files in a directory in a single column and pipes this result to wc -l that counts the number of lines. This is a common way to see the number of files in a directory.

cd

cd /tmp
Change the current working directory to /tmp.
cd ..
Move up one level in the directory path.
cd ../../..
Move up three levels in the directory path.
cd /
Change the current working directory to the root directory.
cd
Change the current working directory to your home directory.

pwd

pwd
Display the current working directory.

df

df
Display file system disk space usage.
df .
Display file system disk space usage for the current directory.
df -h
Display in human readable format.
df -T
Display filesystem type.

du

du temp.txt
Displays the size of the file temp.txt.
du /tmp
Recursively displays the disk usage of all directories and files under /tmp.
du -h /tmp
Displays disk usage in a human readable format.
du -sh /tmp
Displays a summary of disk usage as opposed to a long list of the disk usage of every directory and file.
du -sh
Displays the total disk usage of the current working directory

history

history
Show a history of recently issued commands for this user

!

!!
Repeat the last command issued
!2030
history gives a numerical identifier for each command. The syntax above repeats the command with the identifier 2030.
!ls
Repeat the last command starting with the characters ls

echo

echo "hello world"
Display the text "hello world". Used to create output for scripts.
echo $PS1
Print the current value of the environment variable PS1.
echo *
Print the names of all the files and directories in the current working directory with spaces between them. Often used for scripting when you want to loop through the files in a directory.
echo *.jpg
Print the names of all the jpg files in the current directory.
echo "script failed" > error.log
Redirect the output of the echo command to a file.
echo "script failed" >> error.log
Append to the output file as opposed to overwriting it.

cat

cat temp.txt
Display the contents of the file temp.txt.
cat temp.txt > temp2.txt
Concatenate the contents of the file temp.txt to the file temp2.txt.
cat temp.txt temp2.txt
Display the contents of the files temp.txt and temp2.txt.

more

more temp.txt
Display the contents of the file temp.txt one screen at a time. Used to interactively look through a file.

less

less temp.txt
Display the contents of the file temp.txt one screen at a time. Has a more powerful interface than more that allows you to go forwards, go backwards and search through the file.

cp

cp temp.txt temp2.txt
Copy the file temp.txt to temp2.txt.
cp temp.txt /temp
Copy the file temp.txt to the directory /temp.
cp -rf /home/me /media/backups/
Recursively copy the directory /home/me to the directory /media/backups/.
cp -rfp /home/me /media/backups/
Recursively copy the directory /home/me to the directory /media/backups/ and preserve the original file permissions.
cp -i ftp.txt ftp.sh
Copy the file ftp.txt to ftp.sh and confirm with the user before overwriting an existing file. Here i stands for interactive.

mv

mv tema.txt temp.txt
Move the file tema.txt to temp.txt
mv temp.txt /tmp
Move the file temp.txt to the directory /tmp
mv /home/me/data /tmp/data
Move the directory /home/me/data to the directory /tmp/data
mv -i temp.txt /tmp
Interactive mode. Confirm with the user before overwriting.
mv -f temp.txt /tmp
Force overwrite without prompting the user.
mv -u index.html /web/
Overwrite only if the source file is newer than the destination file

rm

rm file.txt
Remove a file
rm -f /tmp/*
Remove the files in the /tmp directory and don't prompt the user for confirmations
rm -rf /tmp
Completely remove the directory /tmp and all subdirectories and files under /tmp without prompting the user. The parameter r stands for recursive and is a somewhat dangerous parameter to set.
rm -i /tmp/*
Remove all files in /tmp and prompt the user to confirm each deletion

chown

chown root:rootg file1
Change the ownership of file1 to be root user and rootg group. This also works on directories.
chown -R root:rootg /tmp
Recursively change the ownership of all files and directories under /tmp to root user and rootg group.

diff

diff file1 file2
Display the differences between two files.
diff -iW file1 file2
Ignore case and all white space.

find

find . -name *.txt
Find all text files. Performs a recursive search from the current directory.
find . *
Find all files under the current directory.
find . -type f -empty
Find all empty files under the current directory.
find . -type d -empty
Find all empty directories under the current directory.

grep

find . * | grep "hello"
Find files that contain the text "hello". Grep searches within a file and is often used in combination with find as above.
grep "hello there" *
Find files that contain the text "hello there" in the current directory.

touch

touch hello.txt
Change the access and modification times of hello.txt to the current time.

kill

kill 9090
Terminate the process with id 9090.
kill -9 9090
Force the process with id 9090 to terminate. The id of linux processes can be found with the ps command (below).

ps

ps -ef
Show all currently running processes with details.
ps -ef | grep apache
Show all currently running processes with a name like "apache."

top

top
Display a real time continuously updating view of running processes. top provides an extensive user interface that is a little tricky to learn. However, the default screen shows you what processes are using the most resources such as CPU and memory. This is a way to quickly check the health of your system to terminate processes that are running amok.

killall

killall -9 gimp-2.8
Force all processes with a name like "gimp-2.8" to terminate.
man
man ls
Display the manual pages for the linux command ls. In theory, all valid commands on your linux instance have installed man pages.

whoami

whoami
Print the currently logged in user.

crontab

crontab -e
Edit the schedule of cron jobs for this user. Cron is a system process that runs commands according to a schedule.

nohup

nohup command
Run a command in the background.
nohup command &>/dev/null &
Push a command into the background and discard its output.
Nohup stands for "no hangup." This detaches a command from your terminal emulator so that the process will survive when your terminal session ends.

export

export
Print all environment variables.
export | grep PWD
Search for an environment variable called PWD
export -p
Print all environment variables for the current shell.
export SOMETHING="great"
Create an environment variable called SOMETHING and set its value to "great." This can now be accessed from any command or script in the same environment. For example, you can confirm the export with echo $SOMETHING.
export -n SOMETHING
Remove the environment variable SOMETHING.

ln

ln -s target link-name
Create a symbolic link from link-name to target.

mkdir

mkdir directory-name
Create a directory
mkdir -m 777 directory-name
Create a directory and initialize its permissions to 777.
mkdir -p food/japanese/sushi
Create the directory structure food/japanese/sushi

netstat

netstat -an
Display all network connections.
netstat -an | grep 8080
Display any connections on port 8080.
netstat -at
Display all TCP connections.
netstat -s
Display networking statistics.

ping

ping hostname
Send a ping request to a hostname such as the domain name of a website. This displays the IP of the host and the time it takes to received a response to the ping. This is a way to measure network latency.

traceroute

traceroute hostname
Attempts to trace the route that a request will take to a host and display time estimates for each hop on the route. Used to debug networking issues.

passwd

passwd
Change the password of the current user. You will be prompted for your current password and a new password that needs to be entered twice to detect typos.
passwd apache
Change the password for the user apache.
shutdown -h now
Power-off the box now.
shutdown -h +5
Power-off the box in 5 minutes.
shutdown -h 03:00
Power-off the box at 3 AM.
shutdown -c
Cancel a pending shutdown.
shutdown -r
Reboot the machine.

wall

wall message
Displays a message to all terminal sessions across all users.

tar

tar -cvf home.tar /home/me
Create an archive file with everything under the /home/me directory.
tar -xvf home.tar ./
Extract the archive home.tar to the current directory.
tar -cvzf home.tar.gz /home/me
Create a tar file that is compressed with gzip.

gzip

gzip big.log
Compress the file big.log. This replaces the file with the zip file big.log.gz.
gzip -c big.log > big.log.gz
Compress to a new file without overwriting the target.

gunzip

gunzip big.log.gz
Deflate the compressed file big.log.gz to big.log.

tail

tail file.txt
Show the last few lines of file.txt
tail -f nohup.out
Show the last few lines of the file nohup.out and continue to show any updates that are appended to the end of the file. Often used to monitor a log file.

vi

vi file.txt
A non-graphical text editor that has a rich set of features. Considered powerful but is somewhat difficult to learn. For example, vi has an editing mode and a command mode. If you accidentally paste text when you are in command mode, vi will try to interpret the text as a series of commands. This could have interesting results.

vmstat

vmstat
Display current memory statistics.
vmstat 5
Continue to display current memory statistics with an update every 5 seconds.
vmstat -s
Another view with more statistics about processes, memory and cpu usage. This view doesn't auto-refresh.
vmstat -D
Display disk usage statistics.

wget

wget url
Download a URL to the current directory.
wget url -O filename
Download a URL and save it to filename.
wget url --no-check-certificate
Do not validate the URL's SSL certificate. Often used for local testing where certificates may not work.

xargs

find . -name *.log | xargs grep "user id 7799989"
The command above searches for text in all .log files in a directory tree. The xargs command is not used alone but is used to pass arguments to other commands.

apt-get

apt-get is a package management system for Debian, Ubuntu and related Linux distributions that is commonly used to add and remove software packages. It doesn't do a great job of managing package dependencies but is a convenient administrative tool nonetheless.
apt-get update
Update lists of available packages from their sources. This should be done regularly to ensure you are installing the latest software from repositories listed in the file /etc/apt/sources.list.
apt-get upgrade
Install the newest versions of all packages currently installed. This is often done in an attempt to resolve dependency problems on a box. Always perform update before upgrade.
apt-get install package-names
Remove packages.
apt-get purge package-names
Remove packages and try to clean up directory structures and configuration files.

apt-cache

A companion to apt-get that doesn't change your system but provides information about installed and available packages. Often used to look up package names.
apt-cache policy package-name
Display information for a package including whether it is currently installed and if it is available from repositories.
apt-cache search java
Search all package names and descriptions for the text "java."

date

date
Display the current date.
date --date="1 day ago"
Display past dates. This can be used to pass date arguments to commands.
date -d "+10 days"
The future date ten days from now to the second.
date +"%Y/%m/%d"
Output the date in the format 2020/04/30.

ssh

ssh is a tool for logging on to a remote machine. It provides secure two way communication between hosts. As such, a variety of other linux commands are built on top of ssh.
ssh user@host
Connect to the ssh server as a client on the default port. Prompts for a password.
ssh -p 8080 user@host
Specify that the server is running on a nonstandard port.

sftp

sftp user@host
A file transfer tool that uses ssh for encryption. Provides an interactive environment with commands for exploring remove file systems, downloading and uploading files.

scp

scp source-file user@host:destination-folder
Copies files remotely using ssh for security. Prompts for passwords. As with cp, wildcards can be used to specify source files such as *.jpg.

gpg

gpg is an encryption and signing services tool.
gpg -c --no-use-agent --passphrase $password backup.tar.gz
Encrypt the file backup.tar.gz using a password. This form is useful in a script where you are encrypting a backup for archiving but isn't particularly secure.
gpg --encrypt --recipient 'Somebody' target.tar
This will prompt you for a password before encrypting the file target.tar.
gpg --output target.tar --decrypt target.tar.gpg
Decrypt an encrypted file. Prompts for password.

last

last -n 5
Show the last five logons. Includes details such as user and remote host.

nice

nice is a command to alter the priority of a process. This is often used to run a heavy process with low priority so that it doesn't impede other uses of a box. Niceness ranges from -20 to 19 with bigger numbers slowing a process down and small numbers speeding it up, in theory.
nice
Print the current default niceness.
nice --adjustment=-10 ./heavy.sh
Run the script heavy.sh with a low priority.
nice --adjustment=10 ./heavy.sh
Run the script heavy.sh with a high priority.
nohup nice -n -10 ./heavy.sh / > out.txt &
Run the script ./heavy.sh as somewhat high priority in the background and redirects its output to out.txt.

renice

renice changes the priority of a running command.
renice -n 19 -p 8004
Change the priority of the process with id 8004 to the lowest possible priority. Useful when a process is slowing down a box.

stat

stat file.txt
Display detailed information about a file.
stat -f /tmp
Display detailed information about a file system.

tee

tee writes standard output to a file without interrupting standard output. For example, it allows you to write standard output to a file while displaying it at the same time.
command | tee command.log
Execute command and write the output to command.log without disrupting the output to the screen.
command | tee -a command.log
Append to the output file as opposed to overwriting it.

uname

uname -a
Print a variety of information about this linux instance including machine, kernel, node name and operating system version.
uname -p
Print the processor type.

users

users
Print the users that are currently logged into the system.

Linux Commands

This is the complete list of articles we have written about linux commands.
ls
shutdown
xargs
If you enjoyed this page, please consider bookmarking Simplicable.