Congren’s Notes
Last update at 01:20 on November 12, 2023
Chapter 1 Linux Commands
This chapter contains frequently-used commands, and some shortcuts for shell in Linux.
1.1 Frequently-used Commands
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.
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
If you want to upload or download files with key authentication, you can do:
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/.