I – How to create a repertory in Linux from terminal

mkdir -p <folder name>

mkdir -p would not give an error if the directory already exists and the contents for the directory will not change.

II – How to create a file 

Create an empty json file named foo.json:

touch foo.json

//or

cat > foo.json

Add data and press CTRL+D to save the foo.json when using cat on Linux

How to create a text file using the CAT command.

To create a text file named sales.txt, type the following command and then press [Enter] key:

cat > sales.txt

Now type your lines of text. For example:

Month, Profit
01/Apr/2004, $1500
01/May/2004, $1600
01/Jun/2004, $1450

When done and you need to save and exit, press Ctrl+D to return to the bash shell prompt.

To view file use cat or more/less command:

cat sales.txt

//Or
more sales.txt
Creating a file in Linux using the echo or printf

Let us create a file called quote1.txt

echo "While I thought that I was learning how to live, I have been learning how to die." > quote1.txt

//or 

printf 'Study nature, love nature, stay close to nature. It will never fail you.\n' > quote2.txt

ps: You can also redirect the output of a command to a file:

$ cat file > copy_file

...or append to it

$ cat file >> copy_file

Use the the >> instead of > to append data to existing file and to avoid overwriting files.

Ps: If you need root permission:

 sudo sh -c 'cat file > copy_file'

III – How to Remove (Delete) Directory 

Be extra careful when removing files or directories from the command line because once the directory is deleted using the commands, it cannot be fully recovered.

On most Linux filesystems, deleting a directory requires write permission on the directory and its content. Otherwise, you will get “Operation not permitted” error.

Directory names with a space in them must be escaped with a backslash (/).

Removing Directories with rmdir #
rmdir dir1

If the directory is not empty, you will get the following error:

rmdir: failed to remove 'dir1': No such file or directory

In this case, you will need to use the rm command or manually remove the directory contents before you can delete it.

Removing Directories with rm #
rm -r dir1

If a directory or a file within the directory is write-protected, you will be prompted to confirm the deletion. To remove a directory without being prompted, use the -f option:

rm -rf dir1

To remove multiple directories at once, invoke the rm command, followed by the names of the directories separated by space. The command below will remove each listed directory and their contents:

rm -r dir1 dir2 dir3

To Search in a File

To print any line from a file that contains a specific pattern of characters, in our case thumb in the file syslog, run the command:

grep thumb syslog

Grep will display every line where there is a match for the word thumb. When executing this command, you do not get exact matches. Instead, the terminal prints the lines with words containing the string of characters you entered. Here is an example:

To Search in Multiple Files

To search multiple files with the grep command, insert the filenames you want to search, separated with a space character.

In our case, the grep command to match the word phoenix in three files sample,sample2, and sample3 looks like this example:

grep phoenix sample sample2 sample3
Search All Files in Directory

To search all files in the current directory, use an asterisk instead of a filename at the end of a grep command.

In this example, we use nix as a search criterion:

grep nix *
To Find Whole Words Only

Grep allows you to find and print the results for whole words only. To search for the word phoenix in all files in the current directory, append –w to the grep command.

grep -w phoenix *
To List Names of Matching Files

Sometimes, you only need to see the names of the files that contain a word or string of characters and exclude the actual lines. To print only the filenames that match your search, use the -l operator:

grep -l phoenix *

The output shows the exact filenames that contain phoenix in the current directory but does not print the lines with the corresponding word:

example how to find files that contain a word or string of characters excluding the actual lines

ps:  use the recursive search operator -r to include all subdirectories in your search.

By Shabazz

Software Engineer, MCSD, Web developer & Angular specialist

Leave a Reply

Your email address will not be published. Required fields are marked *