Chapter 1 Linux Commands

This chapter contains frequently-used commands, and some shortcuts for shell in Linux.

1.1 Frequently-used Commands

ls

ls # list files
ls -l # list files with permissions
ls -al # list (hidden) files with permissions

touch

touch demo.txt # create a file
touch demo1.txt demo2.txt demo3.txt # create files

echo

echo "Hello world." > demo.txt # write content into demo.txt

mv

mv demo.txt ~/Desktop # move a file
mv demo.txt demo1.txt # rename a file
mv demo.txt ~/Desktop/demo1.txt # move and rename a file to a new place

cat

cat demo.txt # get the content of a file

It is preferred, when the content is short.

less

less demo.txt # get the contents of a file onto the screen a page

Press Q to quit. It is preferred, when the content is long.

tail

tail demo.txt # check last 10 lines of the content
tail -n 20 demo.txt # check 20 lines of the content
tail -f demo.txt # check the added content to the file consistently

One example of using “tail -f” is when you want to check a log file.

mkdir

mkdir demo # make a directory
mkdir -p demo1/demo2/demo3/demo4 # make a directory recursively

rm

rm demo.txt # delete a file
rm -rf demo # delete a directory  
rm -rf demo1 demo2 demo3 # delete more than one directory.
rm demo1.txt demo2.txt demo3.txt # delete more than one file

cp

cp demo.txt ~/Desktop/ # copy a file to the desktop 
cp demo.txt ~/Desktop/demo1.txt # copy a file and rename it
cp -r demo1 ~/Desktop/demo1 # copy a directory and rename it

tar

tar -zcvf demo.tar.gz ./demo.txt # create an archive (file)
tar -zcvf demo.tar.gz ./demo # create an archive (directory)
tar -zxvf demo.tar.gz # uncompress an archive
tar -zxvf demo.tar.gz -C ./Aaron # uncompress and rename an archive
tar -tf demo.tar.gz # see the files in an archive

A .tar file creates an archive without compressing.
A .gz (gzip) file compresses files without creating an archive.
A .tar.gz file archives and compresses multiple files into one archive.
-z: use gzip
-c: create archives
-x : extract archives
-v: display information (not necessary to use)
-f: creates archives with given filename
-C: uncompress archives to a specific place

find

find / -name demo.txt # find a file from the root directory
find ~/Desktop -name demo.txt # find a file from the desktop
find / -name demo* # find any file that starts with demo from the root directory
find / -name *.txt # find any file that ends with .txt from the root directory

If you are using macOS, you have to change the command to find / -name ‘demo.txt’.

scp

scp ~/Desktop/demo.txt root@your_server_ip:/ # upload a file to the server
scp -r ~/Desktop/demo root@your_server_ip:/ # upload a directory to the server
scp root@your_server_ip:/demo.txt ~/Desktop/ # download a file from the server
scp -r root@your_server_ip:/demo ~/Desktop/ # download a directory from the server

To successfully upload or download files or directories to/from a server, you have to use root@your_server_ip to connect the server, instead of using ubuntu@your_server_ip (normally we do to ssh). To do so, you have to change #PermitRootLogin no to PermitRootLogin yes in /etc/ssh/sshd_config. Then, restart the SSH server by

service sshd restart

If you want to upload or download files with key authentication, you can do:

scp -i ~/path/to/your/private/key ~/Desktop/demo.txt root@your_server_ip:/

curl

curl -O https://www.demo.com/demo.pdf ~/Desktop # download a file from the url to the desktop
curl -o wanted_name.pdf https://www.demo.com/demo.pdf
curl ifconfig.me # check public IP address of your Mac

-O (capital o) means that save the file as the same as the downloaded file’s name.
-o means that save the file and name it as you want.

chmod

chmod 644 demo.txt # change permissions for a file (644 is recommended for files)
chmod -R 755 demo # change permissions for a directory (755 is recommended for directories)

More information about 644 and 755, please visit https://linuxhandbook.com/chmod-command/.

grep

grep "Hello" demo.txt # search "Hello" in the demo.txt

1.2 Shortcuts for Shell

Windows users use Ctrl
macOS users use ⌃ Control

  1. Move the cursor to the start of a command: ⌃ Control + A.
  2. Move the cursor to the end of a command: ⌃ Control + E.
  3. Clear the shell screen: ⌃ Control + L.
  4. Clear the current line: ⌃ Control + U.