Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991, by Linus Torvalds. There are lots of commonly and uncommonly used variants of Linux system, such as Ubuntu, Debian, CentOS, Red Hat, Alpine, etc.
Linux is based on Unix, therefore many other Unix-based operating system shares the same commands and system calls with Linux. Notice Android is also based on Linux.
Unlike Windows, Unix-based operating system has a much more useful command line interface. It can be easily used in many action, including inspecting the operating system and hardwares, interacting with file system, creating networks/ssh into remote devices, directly executes codes in a plethora of languages. To do these in windows requires additional steps and much more knowledge into details.
For starters, here is a series of useful commands regarding file system in Linux.
> cd .. #cd stands for change directory > cd /folder #change current directory to someFolder > cd ./folder #change to folder in current directory > cd ../folder #change to folder in parent directory #. stands for current directory #.. stands for parent directory > ls <src> #list file/folder names in src > ls #list file/folder names in current location > ls -A #list all files including hidden ones > ll #list full details of file/folders in current location, such as size and creation date, etc. > pwd #pwd stands for print working directory > mv [-r] <src> <target> #move source to target place > mv *.jpg folder #move all files with suffix jpg to folder > rm [-r -f] <target> #remove target file/folder #option -r means that remove target recursively, which indicates we are removing a folder(recursive structure) #option -f means that we remove target without asking our consent > rm someFile #remove someFile > rm -r someFolder #remove someFolder > rm -rf someFolder #remove someFolder without asking > cp <src> <target> #copy source to the target place > cp <src1> <src2> ... <targetFolder> #group copying
If you want to know something better, you can run the manual command:
> scp man usage: scp [-346BCpqrTv] [-c cipher] [-F ssh_config] [-i identity_file] [-J destination] [-l limit] [-o ssh_option] [-P port] [-S program] source ... target #Here, the contents in the brackets are the option flags, and the following contents are the required fields. > man scp #This will show the manual page of scp with full details. You can close it by pressing q
You can also compress / decompress / archive files in Linux:
> tar -cvf <tarName> <src1> <src2> ... #create an archive of the files src1, src 2 ... named with tarName > tar -xvf <tarFile> #extract files from the tarFile > tar -cvzf #compress using gzip > tar -xvzf #extract from gzip format file > zip <src1> <src2> ... #compress src1, src2... into a zip file > unzip <zipFile> #unzip a zip file
You can also check the running processes:
> ps #check the process of current shell PID TTY TIME CMD 36144 ttys000 0:00.03 /Applications/iTerm.app/Contents/MacOS/iTerm2 --server 36146 ttys000 0:01.11 -fish PID – the unique process ID TTY – terminal type that the user is logged into TIME – amount of CPU in minutes and seconds that the process has been running CMD – name of the command that launched the process > ps -A #view all running processes > ps -e #view all running processes > ps -a #view processes not associated with terminal > ps -x #view processes owned by you > ps -r #view running processes > kill <pid> #kill a process by its id
You can search where the executable file of a command comes from:
> which cpp #returns the location of cpp executables /usr/bin/cpp > which python cpp #returns locations of both /usr/bin/cpp /usr/bin/python
To stop any commands, please press
Ctrl + C. However, in situations where you have to indicate the end of a line in order to end the program, you have to press
Ctrl + D. In any event where both do not work, open another new terminal, find the process id of the old terminal and using
kill <pid> to kill it.
For instance, you might want to press
Ctrl + C here:
> yes "someString" #used to print infinitely many copies of the given string > yes #print infinitely many of 'y'
Here is another useful trick: pipelining. Pipelining allows you to pass the output of one command as input into another command:
Let’s say you want to delete files ending with txt, but every delete requests requires confirmation of typing ‘y’. Now, the pipelining can really help:
> yes | rm -I *.txt
This will automatically type ‘y’ for you, but be careful when using it.
Another useful tool is
apt-get, which allows you to download plethora of programs and tools. In MacOS, the equivalent is
> sudo apt-get search <pkgName> #search related package using the given name > sudo apt-get install <pkgName> #install the package provided by pkgName > sudo apt-get install python3 #install python3 > sudo apt-get remove python3 #uninstall python3 > sudo apt-get update #update package index > sudo apt-get upgrade #upgrade existing package
We now introduce the
sudo command. This is used when we try to run something as administrator, so that we are not seeing any
Permission denied message popping up. Typically, when we run
apt-get commands, we need to use sudo, but this can be avoided by setting.
Now comes three most important commands in terms of communication/downloads from remote places:
> ssh <hostname>@<ipAddress> #host address > scp <src> <dest> #this can be used to download files from remote server to the local computer > wget <url> #download files from url
For editing and creating files, use the following commands:
> touch new.txt #create a file called new.txt in the current directory > vim new.txt #using vim to edit new.txt, if does not exist, then create this file and then edit > nano new.txt #using nano to edit, same as above
For vim, you can install add-ons and using shortcuts to conveniently edit stuff. Visit vim commands.
I will update more stuff if necessary. You can basically work without a cursor only using command line and a keyboard!