The Linux Commands Series: Part II

TechRepublic

I intend to write a series of total VI blog posts describing the most used Linux Commands. This blog is the second of the series. I hope you all enjoy the reading.

Citation/References/Credit will be mentioned at the end of the post.

find

The find command can be used to find files and folders matching a particular search pattern recursively.

Examples:

Find all the files under the current tree that have the .java extension and print the relative path of each file matching:

find . -name ‘*.java’

It’s important to use quotes around special characters like * to avoid the shell interpreting them.

Find directories under the current tree matching the name “code”:

find . -type d -name code

Note: In the above command we are not using quotes around code because it is not a special character.

To find files use -type f and to find links use -type l.

Note: -name performs a case-sensitive search. To perform case-insensitive search use -iname.

You can also search under multiple trees like below:

find folderA folderB -name filename.txt

Find directories under the current tree matching the names nodeA or nodeB:

find . -type d -name nodeA -or -name nodeB

You can also exclude a path in the search using -not -path:

find . -type d -name ‘*.md’ -not -path ‘nodeA/*’

To search for files that have more than 1000 characters (bytes) in them:

find . -type f -size +1000c

To search files bigger than 1KB but smaller than 10MB:

find . -type f -size +1k -size -10M

To search files that were edited more than 5 days ago:

find . -type f -mtime +5

To search files that were edited in the last 24 hours:

find . -type f -mtime -1

You can also delete all the files matching a search by adding the -delete option. The below example deletes all the files that have 1000 characters:

find . -type f -size +1000c -delete

You can also execute a command on each result of the search. In the below example we run the cat command to print the file contents:

find . -type f -exec cat {} \;

{} is filled with the file name at execution time and \; is terminate option.

ln

ln command is used to create links to the file. What is this link? it's a pointer to a file ( a file that points to another file), just like shortcuts in Windows OS.

We have two types of links: hard links and soft links.

Hard links

There are a few limitations to hard links, you cannot create a link to directories and external filesystems(disks).

A hard link syntax:

ln <original> <link>

An example, say you have a file called inventions.txt, you can create a hard link to it using:

ln inventions.txt newinventions.txt

The new hard link is indistinguishable from a regular file:

Anytime we edit one of those files, the content will be updated for both.

Even if the original file is deleted, the content will be available via the hard link as long as the hard link is not deleted.

Soft links

Soft links are different from hard links. Using soft links we can link to other filesystems and to directories, but when the original is removed, the soft link will be broken.

You can create soft links using the -s option of ln:

ln -s <original> <link>

If you delete the original file, the soft link will be broken and the shell will tell you “No such file or directory” if you try to access it.

gzip

You can compress a file using the gzip command.

Example:

gzip filename

This will compress and append a .gz extension to it. The original file is deleted. To prevent deletion and keep the original file intact we can use the -c option and redirection to write output to the destination file name:

gzip -c filename > filename.gz

The -c option specifies that output will go to the standard output stream, leaving the original file intact.

Or you can use the -k option:

gzip -k filename

You can compress multiple files by listing them:

gzip file1 file2

You can compress all the files in a directory recursively using the -r option:

gzip -r filename

Using the -v option actually prints the compression percentage information.

gzip can be used with the -d option to decompress a file:

gzip -d filename.gz

gunzip

The gunzip command is basically equivalent to the gzip command with the -d option enabled always by default.

Example:

gunzip filename.gz

The above command will remove the .gz extension and put the result in the filename file. If the file exists, it will overwrite the file.

You can also extract to a different filename using the -c option and redirection:

gunzip -c filename1.gz > filename2

tar

The tar command is used to create an archive of multiple files by grouping them into a single file.

The below command creates an archive named archive.tar with the content of filename1 and filename2:

tar -cf archive.tar filename1 filename2

Option -c stands for creating and the f option is used to write to file the archive.

To extract files from an archive in the current folder, use:

tar -xf archive.tar

The x option stands for extract.

To extract to a specific directory:

tar -xf archive.tar -C directoryname

You can list the files in a tar using the -t option:

tar -tf archive.tar

You can also compress an archive file using the z option:

tar -czf archive.tar.gz file1 file2

The above command is like creating a tar archive and running a gzip on it.

To unarchive a gzipped archive, you can use gunzip or gzip -d and then unarchive it or you can just use tar -xf and it’ll do for you:

tar -xf archive.tar.gz

alias

For example, typing a command ls -la might tiring sometimes and you may want to use something simple to represent that command like ll then you can use alias.

Example:

alias ll=’ls -la’

Now we can use ll as a regular command instead of ls -la.

In order to list all the aliases defined then just type alias.

Note: The alias will work until the terminal session is closed.

Note: When there is a variable in the command, using double quotes the variable is resolved at definition time, and using single quotes it’s resolved at invocation time. Those two are different:

alias lsthis=”ls $PWD”

alias lscurrent=’ls $PWD’

$PWD refers to the current folder the shell is into and if you navigate away to a new folder, lscurrent lists the files in the new folder, lsthis still lists the files in the folder you were when you defined the alias.

cat

Command cat is used to print the contents of a file as well as add content to a file.

To print a file’s content to the standard output:

cat filename

You can print the content of multiple files:

cat file1 file2

You can use redirect operator > to concatenate the content of multiple files into a new file:

cat file1 file2 > file3

You can use >> to append the contents of multiple files into a new file or create it if it does not exist.

To see line numbers when printing some output use -n option:

cat -n file1

To add a number to non-blank lines use -b and to remove empty lines use -s.

cat is also used with pipe operator to feed content of a file to a command:

cat file1|commandname

less

The less command shows you the contents of a file in a nice and interactive UI.

Example:

less <filename>

You can quit less session by pressing q.

You can navigate the file contents by using the up and down keys or using the space and b to navigate page by page. You can also jump to the end of the file pressing G and jump back to the start pressing g.

You can also do a word search in the contents using / which does a forward search and use ? and typing a word for backward search.

You can also open a system editor by pressing v.

Pressing the F key enters follow mode or watch mode i.e. when the file is changed by someone like another program, you get to see the changes live. You can use ctrl-C to quit this mode.

You can also open multiple files and navigate through them using :n to go to the next file or :p to go to the previous file.

This is the end of this blog post. I will publish a blog post in this series once a week.

Citation/Reference: flaviocopes.com

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store