[TUTORIAL] Basic Bash commands

Since EndeavourOS is a terminal centric distribution, some basic bash tips are in order.
Here are a few to start things out.

Feel free to add some basic bash tips. Just assume the reader knows nothing about bash commands and write it up based on that premise.

Displaying files and Navigation

When a Terminal window is launched as username, the terminal comes up in that users “home” directory. Which would be /home/username/ .

In this directory, use mkdir to create a temporary directory.

$ mkdir Temp
$ cd Temp
$ touch file1 file2 file3 file4

mkdir ( make directory ) makes a new directory

cd Temp ( change directory ) changes to the specified directory, in this case Temp

touch ( Update the access and modification times of each FILE to the current time.)
if the file doesn’t exist, it creates an empty file with the current time. In our case we created four empty files. See the appropriate man pages for further information.

The current working directory is /home/username/Temp

How do we know what the current working directory is? Most default bash prompts shows:
[username@hostname Temp]$

So the user knows they are in the “Temp” directory but not much more.
To find the full current working directory name

$ pwd
/home/username/Temp

pwd = ( print working directory ) shows where we are in the directory tree.
/ represents the root directory
/home represents the home directory that is on the first level from / ( root )
/home/username is the username directory on the second level from / ( often referred to as the user’s home directory )
/home/username/Temp is on the third level from / and is the current working directory.

So if the ls (ls - list directory contents) command is issued from the /home/username/Temp directory, the contents of that directory will be listed

$ ls
file1  file2  file3  file4

The above simply lists ( ls ) the files in the current working directory (/home/username/Temp) by filename.

$ ls -l
total 0
-rw-r--r-- 1 don don 0 Feb  1 11:26 file1
-rw-r--r-- 1 don don 0 Feb  1 11:26 file2
-rw-r--r-- 1 don don 0 Feb  1 11:26 file3
-rw-r--r-- 1 don don 0 Feb  1 11:26 file4

The -l option lists the same files in the current working directory with additional information such as permissions, ownership, date, and time. The total 0 lists how many files and sub-directories are attached to the /home/username/Temp/ directory.

what if we want to list the files in another directory, but don’t want to change to that directory?

$ pwd
/home/username/Temp
$ ls -l /etc/pacman.d/
total 20
-rw-r--r-- 1 root root 3431 Jan 30 08:44 endeavouros-mirrorlist
drwxr-xr-x 4 root root 4096 Jan 27 10:30 gnupg
drwxr-xr-x 2 root root 4096 Aug 20 12:08 hooks
-rw-r--r-- 1 root root 1317 Jan  6 08:47 mirrorlist

The above shows the files and subdirectories in the /etc/pacman.d directory.
This brings up the convention of relative path and absolute path.

In the ls -l command, no absolute filename path was specified, so ls operated on the relative path. With relative path, commands operate on the assumption that they operate on the current working directory, /home/username/Temp/ in this case.

In the ls -l /etc/pacman.d command, an absolute path was specified, so ls operated on the specified absolute path /etc/pacman.d/ in this case.

Ensure that /home/username/Temp/ is the present working directory.

$ pwd
/home/username/Temp
$ mkdir Transcripts
$ cd Transcripts
$ touch Tutorial1 Tutorial2

The absolute pathname for the new directory will be /home/username/Temp/Transcript. The absolute pathname for these files would be /home/username/Temp/Transcripts/Tutorial1 and /home/username/Temp/Transcripts/Tutorial2

[don@Valhalla Temp]$ ls -l
total 4
-rw-r--r-- 1 don don    0 Feb  1 13:14 file1
-rw-r--r-- 1 don don    0 Feb  1 13:14 file2
-rw-r--r-- 1 don don    0 Feb  1 13:14 file3
-rw-r--r-- 1 don don    0 Feb  1 13:14 file4
drwxr-xr-x 2 don don 4096 Feb  1 13:17 Transcripts

Now we see the directory Transcripts. It is known to be a directory because the first column of the directory entry is d and in my terminal, Transcripts has a blue font.

We have two ways to look at the contents of the Transcripts directory from the /home/username/Temp directory

$ ls -l Transcripts/
total 0
-rw-r--r-- 1 don don 0 Feb  1 13:15 Tutorial1
-rw-r--r-- 1 don don 0 Feb  1 13:16 Tutorial2
$ ls -l /home/username/Temp/Transcripts/
total 0
-rw-r--r-- 1 don don 0 Feb  1 13:15 Tutorial1
-rw-r--r-- 1 don don 0 Feb  1 13:16 Tutorial2

Both give the same results. Obviously relative path names = less typing.
Notice both commands were suffixed with a /

Convention has it when using relative or absolute paths, if the target is a directory, suffix the path with a /

If the target is a filename, do not suffix the filename path with a /

ls -l Transcripts/Tutorial1
-rw-r--r-- 1 don don 0 Feb  1 13:15 Transcripts/Tutorial1

The above command lists the file information for the file Tutorial1 in directory Transcripts relative to /home/username/Temp/

$ mkdir /home/username/Temp/Completed-Tutorials/
$ cd /home/username/Temp/Completed-Tutorials/
$ touch Completed_turorial1 Completed_turorial2

To recursively list the contents of the /home/username/Temp/ directory, use the Recursive option -R

$ ls -Rl /home/username/Temp/
total 8
drwxr-xr-x 2 don don 4096 Feb  1 13:42 Completed-Tutorials
-rw-r--r-- 1 don don    0 Feb  1 13:14 file1
-rw-r--r-- 1 don don    0 Feb  1 13:14 file2
-rw-r--r-- 1 don don    0 Feb  1 13:14 file3
-rw-r--r-- 1 don don    0 Feb  1 13:14 file4
drwxr-xr-x 2 don don 4096 Feb  1 13:17 Transcripts

./Completed-Tutorials:
total 0
-rw-r--r-- 1 don don 0 Feb  1 13:42 Completed_turorial1
-rw-r--r-- 1 don don 0 Feb  1 13:42 Completed_turorial2

./Transcripts:
total 0
-rw-r--r-- 1 don don 0 Feb  1 13:15 Tutorial1
-rw-r--r-- 1 don don 0 Feb  1 13:16 Tutorial2

The above command lists the files and directories in the current working directory, then lists the files and directories, if any exists, in the two subdirectories.

$ cd /home/username/Temp
$ mkdir .hidden-file
$ ls -al
total 20
drwxr-xr-x  5 don don 4096 Feb  1 16:42 .
drwx------ 25 don don 4096 Feb  1 16:20 ..
drwxr-xr-x  2 don don 4096 Feb  1 13:42 Completed-Tutorials
-rw-r--r--  1 don don    0 Feb  1 13:14 file1
-rw-r--r--  1 don don    0 Feb  1 13:14 file2
-rw-r--r--  1 don don    0 Feb  1 13:14 file3
-rw-r--r--  1 don don    0 Feb  1 13:14 file4
drwxr-xr-x  2 don don 4096 Feb  1 16:42 .hidden-file
drwxr-xr-x  2 don don 4096 Feb  1 13:17 Transcripts

The -al option lists the same files, but now includes the . (dot) files.
Notice along with .hidden-file there is also:
drwxr-xr-x 5 don don 4096 Feb 1 16:42 .
drwx------ 25 don don 4096 Feb 1 16:20 …

What are these for? We are in directory /home/username/Temp/

drwxr-xr-x  5 don don 4096 Feb  1 16:42 .

the . (dot) on the end = the current working directory or /home/username/Temp/ in this case

drwx------ 25 don don 4096 Feb  1 16:20 ..

the (dot dot) on the end = one directory above or (closer towards the / ( root) directory) the current working directory. In other words the current working directory is
/home/username/Temp/ so (dot dot) equals /home/username/

What is the significance of dot and dot dot?

If the current working directory is /home/username/Temp/Transcripts and we want to go back to /home/username/Temp, we could enter

$ pwd
/home/username/Temp/Transcripts
$ cd ..
$ pwd
/home/username/Temp/

If we want to go back two directories towards / then

$ pwd
/home/username/Temp/Transcripts
$ cd ../..
$ pwd
/home/username

The current working directory is /home/username/Temp/Transcripts/ and we wish to go to
/home/username/Temp/Completed-Tutorials/

$ cd /home/username/Temp/Transcipts
$ pwd
/home/username/Temp/Transcipts
$ cd ../Completed-Tutorials
$ pwd
/home/username/Temp/Completed-Tutorials

Assume there is a thumb drive named USB-DRIVE mounted on /mnt
There is a file on /mnt/USB-DRIVE named copyme and it is necessary to copy this file to /home/username/Temp

$ cd /home/username/Temp/
$ pwd
/home/username/Temp
$ cp /mnt/USB-DRIVE/copyme .
$ ls -l
total 8
drwxr-xr-x 2 don don 4096 Feb  1 13:42 Completed-Tutorials
-rw-r--r-- 1 don don    0 Feb  1 17:05 copyme
-rw-r--r-- 1 don don    0 Feb  1 13:14 file1
-rw-r--r-- 1 don don    0 Feb  1 13:14 file2
-rw-r--r-- 1 don don    0 Feb  1 13:14 file3
-rw-r--r-- 1 don don    0 Feb  1 13:14 file4
drwxr-xr-x 2 don don 4096 Feb  1 13:17 Transcripts

The cp command will copy the file /mnt/USB-DRIVE/copyme to . which represents the current working directory.

Any questions about any of the commands used above, see the corresponding man page. Such as:
$ man ls
for the ls command.

Pudge

10 Likes