Wednesday, 31 January 2018

10 Essential Linux Commands For Navigating Your File System


Introduction


This guide lists 10 Linux commands that you need to know in order to be able to navigate around your file system using the Linux terminal.

It provides commands to find out which directory you are in, which directory you were previously in, how to navigate to other folders, how to get back home, how to create files and folders, how to create links

01. Which Folder Are You In


When you open a terminal window the first thing you need to know is where you are in the file system.

Think of this like the "you are here" marker that you find on maps within shopping malls.

To find out which folder you are in you can use the following command:

pwd

The results returned by pwd may differ depending on whether you are using the shell version of pwd or the one installed in your /usr/bin directory.

In general, it will print something along the lines of /home/username.

02. What Files And Folders Are Under The Current Directory


Now that you know which folder you are in, you can see which files and folders are under the current directory by using the ls command.

ls

On its own, the ls command will list all the files and folders in the directory except for those beginning with a period (.).

To see all the files including hidden files (those starting with a period) you can use the following switch:

ls -a

Some commands create backups of files which begin with the tilde metacharacter (~).

If you don't want to see the backups when listing the files in a folder use the following switch:

ls -B

The most common use of the ls command is as follows:

ls -lt

This provides a long listing sorted by modification time, with the newest first.

Other sort options include by extension, size, and version:

ls -lU
ls -lX
ls -lv

The long listing format gives you the following information:

◈ permissions
◈ number of inodes for the file (see hard links)
◈ owner
◈ primary group
◈ file size
◈ last access time
◈ file/folder/link name

03. How To Navigate To Other Folders


To move around the file system you can use the cd command.

The Linux file system is a tree structure. The top of the tree is denoted by a slash (/).

Under the root directory, you will find some or all of the following folders.

◈ /
◈ bin
◈ boot
◈ cdrom
◈ dev
◈ etc
◈ home
◈ lib
◈ lib64
◈ lost+found
◈ media
◈ mnt
◈ opt
◈ proc
◈ root
◈ run
◈ sbin
◈ srv
◈ sys
◈ tmp
◈ var
◈ usr

The bin folder contains commands that can be run by any user such as the cd command, ls, mkdir etc.

The sbin contains system binaries. 

The usr folder stands for unix system resources and also contains a bin and sbin folder. The /usr/bin folder has an extended set of commands which users can run. Similarly, the /usr/sbin folder contains an extended set of system commands.

The boot folder contains everything required by the boot process.

The cdrom folder is self-explanatory.

The dev folder contains details about all the devices on the system. 

The etc folder is generally where all the system configuration files are stored.

The home folder is generally where all the user folders are stored and for the average user is the only area they should be concerned about.

The lib and lib64 folders contain all the kernel and shared libraries.

The lost+found folder will contain files which no longer have a name which have been found by the fsck command.

The media folder is where mounted media such as USB drives are located.

The mnt folder is also used to mount temporary storage such as USB drives, other file systems, ISO images, etc.

The opt folder is used by some software packages as a place to store the binaries. Other packages use /usr/local.

The proc folder is a system folder used by the kernel. You don't really need to worry about this folder too much.

The root folder is the home directory for the root user.

The run folder is a system folder for storing system runtime information. 

The srv folder is where you would keep things like web folders, ​mysql databases, and subversion repositories etc.

The sys folder contains a folder structure to provide system information.

The tmp folder is a temporary folder.

The var folder contains a whole wealth of stuff specific to the system including game data, dynamic libraries, log files, process IDs, messages and cached application data.

To navigate to a particular folder use the cd command as follows:

cd /home/username/Documents

04. How To Navigate Back To The Home Folde


You can get back to the home folder from anywhere else in the system using the following command:

cd ~

05. How To Create A New Folder


If you want to create a new folder you can use the following command:

mkdir foldername

06. How To Create Files


Linux provides an incredible number of ways for creating new files.

To create an empty file you can use the following command:

touch filename

The touch command is used to update the last access time for a file but on a file that doesn't exist it has the effect of creating it.

You can also create a file using the following command:

cat > filename

07. How To Rename And Move Files Around The File System


The are a number of ways to rename files.

The simplest way to rename a file is to use the mv command.

mv oldfilename newfilename

You can use the mv command to move a file from one folder to another as well.

mv /path/of/original/file /path/of/target/folder

If you want to rename a lot of files which match a similar pattern you can use the rename command.

rename expression replacement filename(s)

For example:

rename "gary" "tom" *

This will replace all files in the folder with gary in it with tom. So a file called garycv will become tomcv.

Note that the rename command doesn't work on all systems. The mv command is safer.

08. How To Copy Files


To copy a file using Linux you can use the cp command as follows.

cp filename filename2

The above command will copy filename1 and call it filename2.

You can use the copy command to copy files from one folder to another.

For example

cp /home/username/Documents/userdoc1 /home/username/Documents/UserDocs

The above command will copy the file userdoc1 from /home/username/Documents to /home/username/Documents/UserDocs

09. How To Delete FIles And Folders


You can delete files and folders using the rm command:

rm filename

If you want to remove a folder you need to use the following switch:

rm -R foldername

The above command removes a folder and its contents including sub-folders.

10. What Are Symbolic Links And Hard Links


 A symbolic link is a file that points to another file. A desktop shortcut is basically a symbolic link.

You might for example have the following file on your system.

◈ /home/username/document/accounts/useraccounts.doc

Maybe you want to be able to access that document from the home/username folder.

You can create a symbolic link using the following command:

ln -s /home/username/documents/accounts/useraccounts.doc /home/username/useraccounts.doc

You can edit the useraccounts.doc file from both places but when you edit the symbolic link you are actually editing the file in the /home/username/documents/accounts folder.

A symbolic link can be created on one filesystem and point to a file on another file system.

A symbolic link really just creates a file which has a pointer to the other file or folder.

A hard link however creates a direct link between the two files. Essentially they are the same file but with just another name.

A hard link provides a good way of categorising files without taking up further disk space.

You can create a hard link using the following syntax:

ln filenamebeinglinked filenametolinkto

The syntax is similar to that of a symbolic link but it doesn't use the -s switch.

Related Posts

0 comments:

Post a Comment