Useful Linux Commands

These Linux commands are used on an almost daily basis for any user.

Special Characters

~ represents the user’s home directory
. the current directory
.. the previous directory
| used to chain commands together

Navigating and Manipulating the File System

pwd

Print the current working directory

$ pwd
/home/jwolfe

cd

Change directory
Move to a certain directory
$ cd /public_html
Move to the directory above the current one
$ cd ..

ls

List files and directories
Show all files and details
$ ls -la

cp

Copy a file or directory from “source” to “target”
$ cp myfile copyoffile

mv

Move/rename a file or directory from “source” to “target”
$ mv oldfile newfile

rm

Remove a file
$ rm uneededfile

mkdir

Make a new directory
$ mkdir newdir

rmdir

Remove a directory
$ rmdir newdir

find

Find a file
$ find new.txt
new.txt

Find all directories in /tmp that are older than 60 minutes and list them
$ find /tmp/. -maxdepth 1 -mmin +60 -type d -exec /usr/bin/ls -la {} \;

File Contents

cat

Print the contents of a file
$ cat /logs/20190504.log

less

A command for perusing the contents of a file without loading the entire file into memory or locking it. Better than more
$ less /logs/20190504.log

head

Show the contents at the beginning of a file
To see a specific number of lines use the - parameter
$ head -50 /logs/20190504.log

tail

Show the contents at the end of a file
To see a specific number of lines use the - parameter
$ tail -50 /logs/20190504.log
To constantly monitor the end of a file that is being written to use the -f parameter
$ tail -f /logs/20190504.log

diff

Compare two files
$ diff thisfile thatfile

grep

Find text in a file
$ grep 1234 /logs/20190504.log

wc

Count the lines in a file
$ wc /logs/20190504.log

vi

Edit a file

tar

Bundle or Extract a bunch of files into or from a “tarball”
The -c option creates a file. The -f option specifies the name of the file. -v means verbose mode which means it prints the files
$ tar -cvf 2019/201901.tar 201901*
The -x extracts the contents to the specified location
$ tar -xvf 2019/201901.tar 2019/.

gzip

gzipCompresses a file usually containing the extension .gz
gunzipUncompresses a file usually containing the extension .gz

$ ls -lh 2019*
total 6.5M
-rw-rw-r– 1 jwolfe jwolfe 6.5M May 4 21:36 201901.tar

$ gzip 2019/201901.tar

$ ls -lh 2019*
total 676K
-rw-rw-r– 1 jwolfe jwolfe 673K May 4 21:36 201901.tar.gz

zip

zipCompresses a file usually containing the extension .zip. gzip is much better when compressing a huge number of files.
unzipUncompresses a file usually containing the extension .zip

wget

A network downloader
$ wget https://www.trustedsite.com/package.tar.gz

history

Shows all commands that the user has entered. When the session ends, it saves the most recent 1,000 commands

Command Tools

crontab

Schedule commands to run at a specific time based on a combination of minute, hour, day of week, day of month, and month.
Option -l lists the crontab file. -e edits the crontab file
$ crontab -l
The -u option with sudo can be used to view/edit another users crontab file
$ sudo crontab -e -u jwolfe

which

Shows the full path of shell commands. Since the crontab file needs to have the full path for all command and files/folders, you may need to use this to find where the command is in your $PATH
$ which rm/usr/bin/rm /usr/bin/rm

whereis

Shows the full path to a shell command and any associated config or other files.
$ whereis php
php: /usr/bin/php /usr/lib64/php /etc/php.d /etc/php.ini /usr/include/php /usr/share/php /usr/share/man/man1/php.1.gz

man

An interface to the on-line reference manuals
$ man find

Profile

alias

A way to make a long command shorter. Usually added the the ~/.bash_profile.
alias ll='ls -laF'

echo

Helpful if you need to print the value of something in your session.
$ echo $PATH
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/jwolfe/.local/bin:/home/jwolfe/bin:/home/jwolfe/.composer/vendor/bin

who

show who is logged on

whoami

Shows the username that your session is currently using. Useful if you have used $ sudo su - otheruser to get to another user’s or root’s profile.

groups

Shows the system groups that the user belongs to.

System Resources

df

Show file system disk space usage. Use the -h option to show the file size in human readable form.
$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/system-root 29G 12G 17G 41% /
devtmpfs 12G 0 12G 0% /dev
tmpfs 12G 0 12G 0% /dev/shm
tmpfs 12G 1.1G 11G 9% /run
tmpfs 12G 0 12G 0% /sys/fs/cgroup
/dev/sda1 497M 271M 226M 55% /boot
tmpfs 2.4G 0 2.4G 0% /run/user/0
tmpfs 2.4G 0 2.4G 0% /run/user/1000
tmpfs 2.4G 4.0K 2.4G 1% /run/user/1006

du

Estimate file space usage. This can be used to find which files/directories are taking up the most space
The -s option to summarize each item. The -h show the file sizes in human readable form
$ du -sh /logs/*

top

Display Linux Processes. Use q to close.

htop

Display Linux Processes as top but, with better GUI, partial bottlenecks tagging and selection of multiple processes. Use F10 to close.

ps

List all current processes that are running.
The -e option selects all processes.
The -f option does full-format listing
Count how many firefox processes are running
$ ps -ef | grep firefox | wc

kill

Stop a running process by specifing the PID of the program (found using the ps command)
Option -15 will send the signal TERM
Option -9 will send the signal KILL
$ kill -15 34452

free

Show information about the system RAM. Shows total amount, used, cached, and available. Use the -h to view the data in human readable form.

Misc

ping

Sends a signal to a remote system to see if it is responding. May be blocked by firewalls or other network settings.

SSH

Start a new session on a remote system.

rsync

Synchronize/move files between two directories. It can use SSH to connect to remote systems.
It is common to use the option -avz which preserves permission and ownership info, compresses the file during transfer and prints the files as they are moved.
The command below is commonly used to move files using the user jwolfe and log the activity.
/usr/bin/rsync -avz --remove-source-files -e "ssh -i /home/jwolfe/rsync-key" --log-file=/logs/`date +"%Y%m%d"`.log /logs/* jwolfe@wolfeplanet.net:/logs

If you need to create a rsync connection with no password (rsync passwordless), to be use in cronjobs for instance see the following post: https://www.thegeekstuff.com/2011/07/rsync-over-ssh-without-password/

passwd

Changes the user’s password.

sudo

Executes a command as the root user. Use this only when NEEDED
$ sudo systemctl restart httpd
It can be used to start a session as another user
$ sudo su - jwolfe
or as the root user
$ sudo su -

systemctl

Controls system services
$ sudo systemctl restart httpd

exit

Exits out of the current session

shutdown

Shutdown the server
The -r option tells it to reboot
The first number after the options tells it how long to wait before executing.
$ sudo shutdown -r 0

Other Useful Commands

Showing Big files in a current directory
du -hs * | sort -rh | head -5

Moving files older than X days to another folder
find /from/path/ -type f -mtime +7 -exec mv "{}" /to/path/ \;