1.

File and Directory CRUD Navigation Commands

Answer»

CRUD stands for Create, Read, Update, and Delete. CRUD operations are said to be the basic operations on any file or directory or database. Even if you are not a Linux User, file and directory CRUD operations are something that you should be comfortable with.

COMMANDMEANINGEXAMPLE & SYNTAX
ls (list all directories)Lists all the files and directories inside the current directory in which you are.Syntax: $ ls
ls -R Lists all the files and directories inside the current directory as well as all the files and directories of the sub-directories as well.Syntax: $ ls -R
ls -aLists all the files and directories in the current directory and also lists the hidden files (such as .git files). However, this command does not list the files and directories of the sub-directories.Syntax: $ ls -a
ls -alLists files and directories of the current directory along with the details like permissions (read, write, execute), owner, file/dir size, etc.Syntax: $ ls -al
cdThis command is used to move to the root directory.Syntax: $ cd
cd ~Same function as cd i.e. move to the root/home directory. Please note that there is a space between cd and tilde (~) symbol.Syntax: $ cd ~
cd ..Move to one level up directory.Syntax: $ cd ..
cd dirNameMove to a particular directory from the current directory. Note that you can only move down the directory and not to the directories in the above level.

Example: In the command shown on the right, we move from the root directory to Desktop.


Syntax: $ cd Desktop


mkdirThis command creates a directory.Example: The command shown in the right will create a directory named “exampleDir” in the current directory in which we are.
Syntax: $ mkdir exampleDir
cat > fileNameThis command creates a file in the current directory.Example: The command shown in the right creates a new file in the current directory and the name of the file will be file1 with an extension of ‘.txt’.
Syntax: $ cat > file1.txt
cat fileNameThis command displays the content in a file. If a file is not present in the current directory, it gives a message showing no such file exists.

Example: The command shown on the right displays the content of the file file1.txt. “Hello there!” is the content inside it.


Syntax: $ cat file1.txt Hello there!
 


cat f1 f2 > f3This command joins the content of two files and stores it in the third file. If the third file does not exist, it is first created and then the joined content is stored.

Example: The command in the right stores the joined content of file1 and file2 in file3. File1 has “Hello there!” and file2 has “What’s up?” in their content. We have displayed the content of file3.


Syntax:


$ cat file1.txt file2.txt > file3.txt
$ cat file3.txt Hello there! What’s up?
rmdir dirNameThis command is the remove directory command. It deletes a directory.

Example: The remove directory command for deleting a directory named “exampleDir” is shown on the right.


Syntax: $ rmdir exampleDir


mv fileName “new file path”This command is the move file command. It moves the file to the new path specified.

Example: The mv command moves the file file1.txt to “Docs” directory.


Syntax: $ mv file1.txt “Docs/”


mv fileName newNameThis command changes the name of the file from the old name i.e. the fileName to the newName. 

Example: The command in the right changes the name of the file file1 to file2.


Syntax: $ mv file1.txt file2.txt


find <starting position to search> <expression determining what to find> <options> <what to find>This command is used for walking a file hierarchy. It is used to find files/directories and perform operations on them. We can search by file, folder, name, creation date, modification date, etc. There are a number of options available. For instance, exec searches the file that meets the criteria and returns 0 as exit status for successful command execution. 

Example: The command in the right is for searching a file with the name file1.txt in the Docs directory.


Syntax: $ find ./Docs -name file1.txt


grep <options> pattern fileNameThe full form of this command is a global search for regular expression and printout. This command searches a file for a particular pattern of characters and displays all the lines that contain that pattern. The pattern being searched is called a regular expression (regex). There are a lot of <options> available. For instance, c is an option that is used to only count the number of lines in the file that matches the pattern.

Example: The command to count the number of lines that have “abc” in them in the file file1.txt is shown on the right.


Syntax: $ grep  -c “abc” file1.txt





Discussion

No Comment Found