Navigating the Filesystem in BASH
From bib. source
The following commands exemplify the Bash syntax and enable users to begin working with the files and directories that make up Linux.
The primary commands, and their syntax, relevant to navigation of the file-system on Linux are (Garn 2022, 11):
| command | purpose | example | result |
|---|---|---|---|
ls | List contents of the current directory | ls /tmp | List the contents of the /tmp directory |
touch | Create a new empty file or update the timestamp of an existing file | touch newfile.txt | Create a new file named “newfile.txt” |
cd | Change from one directory to another | cd /etc | Changes the current directory to /etc |
cat | Display the contents of a text file on the screen | cat data.txt | Display the contents of the data.txt file |
less | Display the contents of a file in windows that fit on the screen | less data.txt | Display the contents of the data.txt file screen at a time when the file would not normally fit on one screen |
tree | Display the directory structure in a tree format | tree /etc | Display the subdirectories and files in the /etc directory in a tree structure |
Correcting the record
It is more accurate to say the purpose of the primary command
lsis to list the files and subdirectories of a directory–that is, to list the contents of a directory, and that when taking no argument it assumes the current directory as argument. Similarly, the purpose of the primary commandcdis to change the current directory to the directory path that is its argument, but when taking no argument it assumes the home directory of the current user as that argument. Further, the true purpose of the primary commandcatis to concatenate the contents of the filepaths it takes as arguments. Nonetheless, because it outputs the result to the screen, it is often used simply to display the contents of any given file in the shell.
Command alternatives
The primary command
lesscan be replaced withmoreormost. The only difference is thatmorehas limited backward navigation compared toless, and thatmostis justlesswith the ability to: (a) open more than one file simultaneously, (b) close one file at a time from those files, (c) lock the open windows for files, and (d) scroll across file windows and not just pages (Rinzwind 2019).
In addition, the command pwd can display to the screen, or print to the shell, the current directory the user is at during the shell session (Linuxize 2019). For example:
pwdCurrent directory as environment variable
Primary commands like
cdandlscan be understood as making use of what current directory the user is at, given certain arguments or the lack thereof. This implies that whatever the current directory is kept track of, likely inside an environment variable.
Finally, the tail primary command takes a filepath or filename as argument, and then displays in the shell, by default, the last lines of the file (Garn 2022, 43). The number of lines to be displayed can be adjusted through command options (to know what they are, refer to the man pages).
kernel operating_system file-system file_path Bourne_Again_Shell bash_script bash_scripting bash shell_script shell_scripting shell linguistics file_path file_paths filepaths directory_path directories subdirectory file home_directory users shell_script shell_scripting bash bash_script bash_scripting Bourne_Again_Shell Bourne_Again_Shell_script Bourne_Again_Shell_scripting environment_variable environment_variables command_line command-line command_lines command-lines Linux
bibliography
- Garn, Damon. The Official CompTIA Linux+ Student Guide (Exam XK0-005). 1.0. Downers Grove, IL: CompTIA, 2022.
- Rinzwind. AskUbuntu, November 26, 2019. https://askubuntu.com/revisions/1191866/3.
- Linuxize. “Pwd Command in Linux (Current Working Directory).” Com, November 15, 2019. https://linuxize.com/post/current-working-directory/.