Essential GNU/Linux Commands (1): Directory - cd, pwd, ls

First things first, you should know what is a directory.

Directory

We have the same thing in Windows called folders. Directories form a hierarchical structure helping human to arrange files. They have nothing to do with how files are stored on disks. Technically, directories are files too, they are files that "link" to other files. In fact, everything in GNU/Linux can be considered as a file.

A directory can have other directories or files under it. The directories or files under it are the children of it, and it is the parent directory of the children directories or files.

  • "/" represents the root directory, the directory with no parent.
  • "." represents the current (working) directory.
  • ".." represents the parent directory of the current directory.
  • "~" represents the home directory of the current user. It is the default current directory when a user login.

Take "/usr/bin/bash" for example, "bash" is a file, "bin" is its parent directory, and "bin" is under "usr", "usr" is under the root directory.


Pathname

"/usr/bin/bash" is "bash"'s pathname. We can access "bash" directly with its pathname. pathnames start with "/" are absolute.

If we change the shell (terminal) current (working) directory to "/usr", we can access "bash" with "bin/bash", this pathname is relative.


Change Directory

cd [DIRECTORY]

Change shell (terminal) current (working) directory. [DIRECTORY] is the destination directory's pathname

For example, "cd /usr/bin" change the current directory to "bin", whose pathname is "/usr/bin".

[texpion@com ~]$ cd /usr/bin
[texpion@com bin]$
If DIRECTORY is omitted, the current directory will be changed to the home directory.

[texpion@com bin]$ cd
[texpion@com ~]$

Change to the Previous Directory

cd -

Display the Working Directory

pwd

Print the working directory('s pathname).

[texpion@com ~]$ pwd
/home/texpion

List Content

ls [DIRECTORY]

list files (including directories) under DIRECTORY. "s" stands for sort.

[texpion@com ~]$ ls ~/myfolder
file1 file2 file3 file4

If DIRECTORY is omitted, contents of the current directory will be listed.

"ls -a" also lists hidden files. Files whose name start with "." are normally hidden.

[texpion@com ~]$ ls ~/myfolder
. .. file1 file2 file3 file4 .hidden-file

Autocomplete

We don't need to type all the characters of commands and pathnames. We can always type the first few letters and press TAB on the keyboard. If there is only one match, the command or pathname will be autocompleted. Otherwise all the commands and the files (directories) whose name starts with these letters will be listed.

[texpion@com ~]$ ls ~/myfo
myfolder myfolk.ogg myfood myfoot.txt

Note: files (including directories) with the same name can not exist under the same directory, but are allowed under different directories.



Related

Comments

Popular

Intel 7th and 8th generation processor graphics driver for Windows 7/8/8.1

AV1 vs VP9 vs AVC (h.264) vs HEVC (h.265): Part II - Fidelity

AV1 vs VP9 vs AVC (h.264) vs HEVC (h.265): Part I - Lossless

AV1 vs VP9 vs AVC (h.264) vs HEVC (h.265): Part IV - Decode

OBS Studio settings for best quality (loseless) recording

AV1 vs VP9 vs AVC (h.264) vs HEVC (h.265): Part III - Quality

YouTube starts delivering videos in AV1 format