Linux — basic navigations
Before we get to the nitty gritty of navigation in Linux, It is important to point out how the files and folders are organized.
Linux files and folders are in hierarchical order, this is, they are arranged in a tree-like structure. The first directory in this hierarchical order is called a root directory. Every other files and folder originate from here. The root folder is usually denoted with a single forward slash ( / ). Below is an example of a Linux file system.
Navigation
To navigate the Linux file system, we’ll cover pwd, cd and ls commands on your terminal.
- pwd — stands for print working directory. It is used to show where you are on the file system. The example below shows where I’m on the file system. The folders above it are: jamal(my user folder), home folder and finally the root folder(shown as / ).
2. cd — stands for change directory. It is used to navigate from one directory to another. To change your directory to the previous folder we use cd .. (the two dots are used to go back up a directory).
To navigate to the next directory on the tree we use cd <nameofdirectory>
You can navigate to the root folder in one step as follows
3. ls — stands for list. It is used to list files or folders that are available in your working directory. It has some options/flags: to view details of the files or folders use the flag -L, to view hidden files use -a, or you can combine the two flags -la.
Looking Around
To look around we’ll use cat, less, file, head and tail commands.
- cat — used to view the contents of a file. This command will show all the contents of a file selected. To use it just type cat <filename> as shown below:
2. less — used to view the contents of a file one screen at a time. To use it just type less <filename>. To control less use :
- PageUp/b — to scroll one page up
- PageDown/space — to scroll one page down
- q — to quit
3. file — used to check the file type. To use it just type file <filetype>.
4. head — used to show the first 10 lines of the file by default. You can use it to read from a single file (head <filename>), or from multiple files (head <filename1> <filename2>). There are flags used to manipulate the behavior of the head. These are:
- -n — used to change the number of lines to show e.g -n 20(to show the first 20 lines of the file’s content)
- -c — used to change the number of bytes to show e.g -c 32(to show the first 32bytes of the file’s content)
- -q — used to suppress the header information when reading from multiple files.
5. tail — works just like a head command. The difference is that tail is used to read from the bottom of a file. By default, it shows the last 10 lines of a file. The flags mentioned in the head all apply to it as well.
Manipulating Files
To manipulate files we’ll use the commands cp, mv, rm, mkdir, rmdir and touch.
- cp — used to copy files and directories.
- to copy file1 into file2 use the command cp file1 file2. if file2 doesn’t exist it will be created for you.
- to copy a file to a folder use the command cp <filename> <directorypath>
- to copy several files use the command cp <wildcard-selection> <destination> e.g cp *.txt <destination>( this will copy all files that end with .txt)
- to copy a folder use the command cp -r <foldername> <destination>. The -r is used to recursively select all the contents of the folder.
2. mv — used to move files and directories, and rename files and directories.
- to move a file or directory use mv <filename> <destination-path> and mv -r <directory> <destination-path>.
- to rename a file or folder simply move them to the same directory they are in e.g mv demo.txt ./test.txt, mv -r directory ./directory2
Note: mv file1 file2 — in this scenario file1 will be renamed to file2 if file2 doesn’t exist. If file2 exists then the contents of file1 will replace the contents of file2.
Get Jamal Guyo’s stories in your inbox
Join Medium for free to get updates from this writer.
3. rm — used to remove a file or directory.
- to remove a file using the command rm <filename>
- to remove a directory use the command rm -r <foldername>
4. mkdir — used to create a new directory. to use it simply type mkdir <directory name>
5. rmdir — used to delete an empty directory. to use it simply type rmdir <directory name>. To delete a non empty directory use rm -r <directory name>.
6. touch — used to create a new file. to use it simply type touch <filename>. e.g touch demo.txt





