(Updating...)Notes for Linux commands

​ I haven’t have had the chance to systematically study the linux commands, although I have used Linux to finish some tasks in the class. And recently I just found the book The Linux Command Line on the Internet, it’s easy-to-read for non-native English speakers so I decide to read the whole book and make some notes.So this article is the notes of the book The Linux Command Line and other useful commands that I came across. I will try to use simple words to cover the contents of the book by omitting unnecessary sentences.

Learning Shell

What is Shell?

When we enter the terminal, we see something like :

1
levick@ubuntu:~$

which represent username@hostname:currentPath$. In this (my) case, my username is $levick$, my hostname(name of the machine) is $ubuntu$, the current path is $ \sim$(home path of current user), $\$$ -> normal user, # -> super user.

BTW, $/$ represent the root directory of the computer. ( It’s kind of like This PC in Windows)

And, to log in super user, type sudo -s.

some really basic commands:

date: get today’s date.

cal: display the date like a calendar.

df: the current amount of free space on your disk drives.

free: display the amount of free memory.

to close the terminal, typeexit.

Change directory in the File System

To navigate the file system, we may use following commands.

  • pwd: display your password display the current directory. (short for Print Working Directory)
  • file: used as file filename, to check the file format of chosen file.
  • ls: list the contents of the current directory.
  • cd: change directory, used as cd PathName.

pwd and file are simple, while ls and cd can be more complex.

The command cd

cd is what we use all the time, because we have to change the directory constantly. The are 2 different methods to represent Path——–>absolute pathname or relative pathname.

absolute pathname: starts from the root directory and leads to its destination

relative pathname: starts from the working directory, uses . to represent the working directory and .. to represent the parent directory of the working directory.

Even more , we can change the working directory easily with following shortcut.

Shortcut Result
cd go to the home directory
cd - go to the previous directory
cd ~user-name go to the home directory of the “user-name”

Cases I Came across

Vim

Replace string that contains \(or /)

​ We know that in command mode we use :%s/string1/string2/g to replace string1 with string2 globally.(Note that / here is a delimiter) But when we need to replace string that contains \ or /, it’s a bit tricky. In general, there are two ways to do it.

​ For example, we want replace all the \ with / (when you want to convert path from Windows to Linux)

  • escape the /, use :%s/\\/\//g. From the first slash to the last slash, they are: delimiter, escape character, character, delimiter, escape character, character, delimiter.
    In this case we use / as a delimiter, so if we don’t want to mix up delimiter / and the character /, we use \/ to escape / the delimiter to / the character.
  • use another delimiter, like # or @, use :%s#\\#/#g. From the first slash to the last slash, they are: escape character, character, character. But note that you can’t use |, \," as a separator.

Note that in both cases we escape \ (the default escape character) as \\ when we want to use it as a character.

​ Always be ware of the structure of the command, distinguish delimiter , escape character and normal character.


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!