Command | Common Usage |
su | su someuserSwitch user to someuser. This will prompt you for someuser's password.suSwitch user to root. |
sudo | sudo lsExecute 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.aliasShows a list of active aliases. If a command is behaving strangely, it may be aliased. |
unalias | unalias lsRemoves an alias associated with the string "ls." |
clear | clearClear the screen of a terminal. |
lsList files in the current working directory.ls /tmpList files in the directory /tmp.ls -lShow more details such as file permissions.ls -tSort files by modification date in descending order with newest files first.ls -trSort 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 -aShow hidden files.ls -altrA 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 -lwc 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 /tmpChange 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.cdChange the current working directory to your home directory. |
pwd | pwdDisplay the current working directory. |
df | dfDisplay file system disk space usage. df .Display file system disk space usage for the current directory.df -hDisplay in human readable format.df -TDisplay filesystem type. |
du | du temp.txtDisplays the size of the file temp.txt.du /tmpRecursively displays the disk usage of all directories and files under /tmp.du -h /tmpDisplays disk usage in a human readable format.du -sh /tmpDisplays a summary of disk usage as opposed to a long list of the disk usage of every directory and file.du -shDisplays the total disk usage of the current working directory |
history | historyShow a history of recently issued commands for this user |
! | !!Repeat the last command issued!2030history gives a numerical identifier for each command. The syntax above repeats the command with the identifier 2030.!lsRepeat the last command starting with the characters ls |
echo | echo "hello world"Display the text "hello world". Used to create output for scripts.echo $PS1Print 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 *.jpgPrint the names of all the jpg files in the current directory.echo "script failed" > error.logRedirect the output of the echo command to a file.echo "script failed" >> error.logAppend to the output file as opposed to overwriting it. |
cat | cat temp.txtDisplay the contents of the file temp.txt.cat temp.txt > temp2.txtConcatenate the contents of the file temp.txt to the file temp2.txt.cat temp.txt temp2.txtDisplay the contents of the files temp.txt and temp2.txt. |
more | more temp.txtDisplay the contents of the file temp.txt one screen at a time. Used to interactively look through a file. |
less | less temp.txtDisplay 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.txtCopy the file temp.txt to temp2.txt.cp temp.txt /tempCopy 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.shCopy 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.txtMove the file tema.txt to temp.txtmv temp.txt /tmpMove the file temp.txt to the directory /tmpmv /home/me/data /tmp/dataMove the directory /home/me/data to the directory /tmp/datamv -i temp.txt /tmpInteractive mode. Confirm with the user before overwriting.mv -f temp.txt /tmpForce 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.txtRemove a filerm -f /tmp/* Remove the files in the /tmp directory and don't prompt the user for confirmationsrm -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 file1Change the ownership of file1 to be root user and rootg group. This also works on directories.chown -R root:rootg /tmpRecursively change the ownership of all files and directories under /tmp to root user and rootg group. |
diff | diff file1 file2Display the differences between two files.diff -iW file1 file2Ignore case and all white space. |
find | find . -name *.txtFind all text files. Performs a recursive search from the current directory. find . *Find all files under the current directory.find . -type f -emptyFind all empty files under the current directory.find . -type d -emptyFind 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.txtChange the access and modification times of hello.txt to the current time. |
kill | kill 9090Terminate the process with id 9090.kill -9 9090Force the process with id 9090 to terminate. The id of linux processes can be found with the ps command (below). |
ps | ps -efShow all currently running processes with details.ps -ef | grep apacheShow all currently running processes with a name like "apache." |
top | topDisplay 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.8Force all processes with a name like "gimp-2.8" to terminate. |
man | man lsDisplay the manual pages for the linux command ls. In theory, all valid commands on your linux instance have installed man pages. |
whoami | whoamiPrint the currently logged in user. |
crontab | crontab -eEdit the schedule of cron jobs for this user. Cron is a system process that runs commands according to a schedule. |
nohup | nohup commandRun 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 | exportPrint all environment variables.export | grep PWDSearch for an environment variable called PWDexport -pPrint 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 SOMETHINGRemove the environment variable SOMETHING. |
ln | ln -s target link-nameCreate a symbolic link from link-name to target. |
mkdir | mkdir directory-nameCreate a directory mkdir -m 777 directory-nameCreate a directory and initialize its permissions to 777.mkdir -p food/japanese/sushiCreate the directory structure food/japanese/sushi |
netstat | netstat -anDisplay all network connections.netstat -an | grep 8080Display any connections on port 8080.netstat -atDisplay all TCP connections.netstat -sDisplay networking statistics. |
ping | ping hostnameSend 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 hostnameAttempts 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 | passwdChange 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 apacheChange the password for the user apache. |
shutdown -h nowPower-off the box now.shutdown -h +5Power-off the box in 5 minutes.shutdown -h 03:00 Power-off the box at 3 AM.shutdown -c Cancel a pending shutdown.shutdown -rReboot the machine. | |
wall | wall messageDisplays a message to all terminal sessions across all users. |
tar | tar -cvf home.tar /home/meCreate 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/meCreate a tar file that is compressed with gzip. |
gzip | gzip big.logCompress the file big.log. This replaces the file with the zip file big.log.gz.gzip -c big.log > big.log.gzCompress to a new file without overwriting the target. |
gunzip | gunzip big.log.gzDeflate the compressed file big.log.gz to big.log. |
tail | tail file.txtShow the last few lines of file.txttail -f nohup.outShow 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.txtA 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 | vmstatDisplay current memory statistics.vmstat 5Continue to display current memory statistics with an update every 5 seconds.vmstat -sAnother view with more statistics about processes, memory and cpu usage. This view doesn't auto-refresh.vmstat -DDisplay disk usage statistics. |
wget | wget urlDownload a URL to the current directory.wget url -O filenameDownload a URL and save it to filename.wget url --no-check-certificateDo 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 updateUpdate 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 upgradeInstall 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-namesRemove packages.apt-get purge package-namesRemove 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-nameDisplay information for a package including whether it is currently installed and if it is available from repositories.apt-cache search javaSearch all package names and descriptions for the text "java." |
date | dateDisplay 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@hostConnect to the ssh server as a client on the default port. Prompts for a password.ssh -p 8080 user@hostSpecify that the server is running on a nonstandard port. |
sftp | sftp user@hostA 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-folderCopies 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.gzEncrypt 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.tarThis will prompt you for a password before encrypting the file target.tar.gpg --output target.tar --decrypt target.tar.gpgDecrypt an encrypted file. Prompts for password. |
last | last -n 5Show 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.nicePrint the current default niceness.nice --adjustment=-10 ./heavy.shRun the script heavy.sh with a low priority.nice --adjustment=10 ./heavy.shRun 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 8004Change 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.txtDisplay detailed information about a file.stat -f /tmpDisplay 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.logExecute command and write the output to command.log without disrupting the output to the screen.command | tee -a command.logAppend to the output file as opposed to overwriting it. |
uname | uname -aPrint a variety of information about this linux instance including machine, kernel, node name and operating system version.uname -pPrint the processor type. |
users | usersPrint the users that are currently logged into the system. |