linux:commands

Commands

When coming up to speed as a Linux user, it helps to have a cheat sheet that can help introduce you to some of the more useful commands.

In the 18 tables below, you’ll find sets of commands with simple explanations and usage examples that might help you or Linux users you support become more productive on the command line.

[ Also see: Invaluable tips and tricks for troubleshooting Linux ]

These commands will help new Linux users become familiar with their Linux accounts.

CommandFunctionExample
pwdDisplays your current location in the file systempwd
whoamiDisplays your username – most useful if you switch users with su and need to be reminded what account you're using currentlywhoami
lsProvides a file listing. With -a, it also displays files with names starting with a period (e.g., .bashrc). With -l, it also displays file permissions, sizes and last updated date/time.ls
ls -a
ls -l
envDisplays your user environment settings (e.g., search path, history size, home directory, etc.)env
echoRepeats the text you provide or displays the value of some variableecho hello
echo $PATH
historyLists previously issued commandshistory
history | tail -5
passwdChanges your password. Note that complexity requirements may be enforced.passwd
history | tail -5

See more commands below this ad.

Linux provides several commands for looking at the content and nature of files. These are some of the most useful commands.

CommandFunctionExample
catDisplays the entire contents of a text file.cat .bashrc
moreDisplays the contents of a text file one screenful at a time. Hit the spacebar to move to each additional chunk.more .bash_history
lessDisplays the contents of a text file one screenful at a time, but in a manner that allows you to back up using the up arrow key.less .bash_history
fileIdentifies files by type (e.g., ASCII text, executable, image, directory)file myfile
file ~/.bashrc
file /bin/echo

These are some Linux commands for changing file attributes as well as renaming, moving and removing files.

CommandFunctionExample
chmodChanges file permissions (who can read it, whether it can be executed, etc.)chmod a+x myscript
chmod 755 myscript
chownChanges file ownersudo chown jdoe myfile
cpMakes a copy of a file.cp origfile copyfile
mvMoves or renames a file – or does bothmv oldname newname
mv file /new/location
mv file /newloc/newname
rmDeletes a file or group of filesrm file
rm *.jpg
rm -r directory

Linux systems provide commands for creating files and directories. Users can choose the text editor they are comfortable using. Some require quite a bit of familiarity before they'll be easy to use while others are fairly self-explanatory.

CommandFunctionExample
nanoAn easy-to-use text editor that requires you to move around in the file using your arrow keys and provides control sequences to locate text, save your changes, etc.nano myfile
viA more sophisticated editor that allows you to enter commands to find and change text, make global changes, etc.vi myfile
exA text editor designed for programmers and has both a line-oriented and visual modeex myfile
touchCreates a file if it doesn't exist or updates its timestamp if it doestouch newfile
touch updatedfile
>Creates files by directing output to them. A single > creates a file while » appends to an existing file.cal > calendar
ps > myprocs
date » date.log
mkdirCreates a directorymkdir mydir
mkdir ~/mydir
mkdir /tmp/backup

The command for moving around the Linux file system is ls, but there are many variations.

CommandFunctionExample
cdWith no arguments, takes you to your home directory. The same thing would happen if you typed cd $HOME or cd ~cd
cd ..Moves up (toward /) one directory from your current locationcd ..
cd <location>Takes you to the specified location. If the location begins with a /, it is taken to be relative to the root directory; otherwise it is taken as being relative to your current location. The ~ character represents your home directory.cd /tmp
cd Documents
cd ~/Documents

There are a number of Linux commands that can help you learn about other commands, the options they offer and where these commands are are located in the file system. Linux systems also provide a command that can help you to learn what commands are available related to some subject – for example, commands that deal with user accounts.

CommandFunctionExample
manDisplays the manual (help) page for a specified command and (with -k) provides a list of commands related to a specified keywordman cd
man -k account
whichDisplays the location of the executable that represents the particular commandwhich cd
aproposLists commands associated with a particular topic or keywordapropos user
apropos account

There are two commands that can help you find files on Linux, but they work very differently. One searches the file system while the other looks through a previously built database.

CommandFunctionExample
findLocates files based on criteria provided (file name, type, owner, permissions, size, etc.). Unless provided with a location from which to start the search, find only looks in the current directory.find . -name myfile
find /tmp -type d
locateLocates files using the contents of the /var/lib/mlocate/mlocate.db which is updated by the updatedb command usually run through cron. No starting location is required.locate somefile
locate “*.html” -n 20

You can easily view processes that are running on the system – yours, another user's or all of them.

CommandFunctionExample
psShows processes that you are running in your current login sessionps
ps -efShows all processes that are currently running on the systemps -ef
ps -ef | more
pstreeShows running processes in a hierarchical (tree-like) display that demonstrates the relationships between processes (-h highlights current process)pstree
pstree username
pstree -h

These commands allow you to display services as well as start and stop them.

CommandFunctionExample
systemctlThe systemctl command can start, stop, restart and reload services. Privileged access is required.sudo systemctl stop apache2.service
sudo systemctl restart apache2.service
sudo systemctl reload apache2.service
serviceLists services and indicates whether they are runningservice –status-all

Linux offers a few commands for terminating processes. Privileged access is needed if you did not start the process in question.

CommandFunctionExample
killTerminates a running process provided you have the authority to do sokill 8765
sudo kill 1234
kill -9 3456
killallTerminates all processes with the provided namekillall badproc
pkillTerminates a process based on its namepkill myproc

The table below lists commands that will display details about the Linux OS that is running on a system.

CommandFunctionExample
unameDisplays information on OS release in a single line of textuname -a
uname -r
lsb_releaseOn Debian-based systems, this command displays information on the OS release including its codename and distributor IDlsb_release -a
hostnamectlDisplays information on the system including hostname, chassis type, OS, kernel and architecturehostnamectl

These are some of the more useful tools for examining system performance.

CommandFunctionExample
topShows running processes along with resource utilization and system performance data. Can show processes for one selected user or all users. Processes can be ordered by various criteria (CPU usage by default)top
top jdoe
atopSimilar to top command but more oriented toward system performance than individual processesatop
freeShows memory and swap usage – total, used and freefree
dfDisplay file system disk space usagedf
df -h

Commands for creating and removing user accounts and groups are fairly straightforward.

CommandFunctionExample
useraddAdds a new user account to the system. A username is mandatory. Other fields (user description, shell, initial password, etc.) can be specified. Home directory will default to /home/username.useradd -c “John Doe” jdoe
useradd -c “Jane Doe” -g admin -s /bin/bash jbdoe
userdelRemoves a user account from the system. The -f option runs a more forceful removal, deleting the home and other user files even if the user is still logged in.userdel jbdoe
userdel -f jbdoe
groupaddAdds a new user group to the system, updating the /etc/group.groupadd developers
groupdelRemoves a user group from the systemgroupdel developers

The commands below help you view network interfaces and connections.

CommandFunctionExample
ipDisplays information on network interfacesip a
ssDisplays information on sockets. The -s option provides summary stats. The -l option shows listening sockets. The -4 or -6 options restrict output to IPv4 or IPv6 connections.ss -s
ss -l
ss -4 state listening
pingCheck connectivity to another systemping remhost
ping 192.168.0.11

There are many aspects to managing security on a Linux system, but there are also a lot of commands that can help. The commands below are some that will get you started. Click on this link to see these and other commands on 22 essential Linux security commands.

CommandFunctionExample
visudoThe visudo command allows you to configure privileges that will allow select individuals to run certain commands with superuser authority. The command does this by making changes to the /etc/sudoers file.visudo
sudoThe sudo command is used by privileged users (as defined in the /etc/sudoers file to run commands as root.sudo useradd jdoe
suSwitches to another account. This requires that you know the user's password or can use sudo and provide your own password. Using the - means that you also pick up the user's environment settings.su (switch to root)
su - jdoe
sudo su - jdoe
whoShows who is logged into the systemwho
lastLists last logins for specified user using records from the /var/log/wtmp file.last jdoe
ufwManages the firewall on Debian-based systems.sudo ufw status
sudo ufs allow ssh
ufw show
firewall-cmdManages the firewall (firewalld) on RHEL and related systems.firewall-cmd –list-services
firewall-cmd –get-zones
iptablesDisplays firewall rules.sudo iptables -vL -t security

Tasks can be scheduled to run periodically using the command listed below.

CommandFunctionExample
crontabSets up and manages scheduled processes. With the -l option, cron jobs are listed. With the -e option, cron jobs can be set up to run at selected intervals.crontab -l
crontab -l -u username
crontab -e
anacronAllows you to run scheduled jobs on a daily basis only. If the system is powered off when a job is supposed to run, it will run when the system boots.sudo vi /etc/anacrontab

The commands for installing and updating applications depend on what version of Linux you are using, specifically whether it's Debian- or RPM-based.

CommandFunctionExample
apt updateOn Debian-based systems, updates the list of available packages and their versions, but does not install or upgrade any packagessudo apt update
apt upgradeOn Debian-based systems, installs newer versions of installed packagessudo apt upgrade
apt listLists all packages installed on Debian-based system. With –upgradable option, it shows only those packages for which upgrades are available.apt list
apt list –installed
apt list –upgradable
apt installOn Debian-based systems, installs requested packagesudo apt install apache2
yum updateOn RPM-cased systems, updates all or specified packagessudo yum update
yum update mysql
yum listOn RPM-based systems, lists packagesudo yum update mysql
yum installOn RPM-based systems, installs requested packagesudo yum -y install firefox
yum listOn RPM-based systems, lists known and installed packagessudo yum list
sudo yum list –installed

Commands for shutting down and rebooting Linux systems require privileged access. Options such as +15 refer to the number of minutes that the command will wait before doing the requested shutdown.

CommandFunctionExample
shutdownShuts down the system at the requested time. The -H option halts the system while the -P powers it down as well.sudo shutdown -H now
shutdown -H +15
shutdown -P +5
haltShuts down the system at the requested time.sudo halt
sudo halt -p
sudo halt –reboot
poweroffPowers down the system at the requested time.sudo shutdown -H now
sudo shutdown -H +15
sudo shutdown -P +5
  • linux/commands.txt
  • Last modified: 2020/08/12 02:54
  • by jimboobrien