In Linux, the command line remains the most flexible and powerful way to perform tasks.

What is a home directory in Linux?

To make things easy, each user is assigned a directory where they can store their personal files.

Article image

This directory is known as a user’s home directory.

Home directories are found under thehomedirectory.

For example, my home directory is/home/himanshu.

like note that a user’s home directory has the same name as their login name.

Users have complete control over their home directory as well as all its sub-directories.

How to check the present working directory?

Whenever you open a command line shell in Linux, you start at your home directory.

This is your present working directory, which changes as you switch to some other directory.

Use thepwdcommand to check the complete path of your present working directory at any point of time.

In this casehimanshu@ubuntu:~/Pictures$is the command line prompt.

How to switch directories?

Use thecdcommand to navigate through the Linux filesystem.

This command requires either a directory name or its complete path depending upon where the directory is present.

In this case, the command line shell will search for thevacationsdirectory insidepictures.

A path relative to the present working directory is also known as relative path.

But in case you want to switch to/home/techspot, you’ll have to trigger the command:cd /home/techspot.

How to view directory contents?

Use thelscommand to list the contents of a directory.

If the command is run without any argument, it displays the contents of the present working directory.

If you observe closely, the output of thelscommand is color coded.

These different colors represent different types of files, making it easy to visually identify them.

How to view the contents of a file?

Use thecatcommand to view the contents of a file.

This command expects a filename as an argument.

However, there is a limitation.

In that case you might use use thelesscommand along with thecatcommand:cat [filename] | less.

To quit the display mode press theqkey.

How to create a new file?

Use thetouchcommand to create a new file.

The command requires a filename as argument.

To create a new file at a location other than the present working directory, use the absolute path.

For example,touch /home/himanshu/practice/test.log.

Tip: To write anything into a newly created file, use a command line editor likeViorVim.

How to rename/copy/delete a file?

Use themvcommand to rename a file.

For example, to renamelog.txttonew_log.txt, spin up the command:mv log.txt new_log.txt.

As always, if the file is not present in the present working directory, use the absolute path.

you’re free to also use themvcommand to move a file from one location to other.

This is the equivalent of a cut-paste operation via GUI.

To copy a file from one directory to another, use thecpcommand.

Like themvcommand,cpalso requires a source and a destination.

For example,cp log.txt /home/himanshuwould create a copy oflog.txt(with the same name) in the/home/himanshudirectory.

To remove a file, use thermcommand.

This command expects a filename as an argument.

To remove directories, use the-rcommand line option with thermcommand.

For example,rm -r /home/himanshu/practice/would delete thepracticedirectory with all its subdirectories and files.

How to search for files?

To search for files within a given directory, use thefindcommand.

The command requires directory path and filename as argument.

you could skip it.

If a directory path is not specified, thefindcommand searches in the present working directory.

it’s possible for you to also usewildcardswith thefindcommand to get the most out of it.

For example, if you want to search all.cfiles present in the/home/himanshu/practicedirectory, use thefindcommand as shown below.

The ‘*’ character is a wildcard that can represent any number of characters.

For example, tech* can represent tech, techspot, techreport, and more.

How to search text within files?

To search text within files, use thegrepcommand.

The command expects a keyword and a filename as arguments, and outputs lines that contain the keyword.

kindly note that unlike thefindcommand, thegrepcommand doesn’t search within subdirectories by default.

What is the auto-complete feature?

Use thetabkey to auto complete these long names and paths easily.

For example, to write/home, you might just write/hoand press tab.

The command line shell will auto complete the name for you.

The shell displayed all the names that it can use for auto completion.

Once done, you could hit thetabkey again to autocomplete the name.

Root is the only user that has control over the entire Linux system.

As it’s possible for you to guess, the root account is mostly used by system administrators only.

It is the same directory that contains thehomedirectory, which further contains user specific directories.

What are man pages?

To open a man page, just run themancommand followed by the command name.

For example, runman rmto launch the manual page of thermcommand.

you might find a lot of useful information about Linux commands this way.

How to access file metadata like size, permissions, and more?

Use thelscommand with-loption to display file metadata in output.

For example, in the line shown above,dindicates this is a directory.

Other values can be:-for normal file,sfor socket file,lfor link file, and more.

The next 9 characters represent permissions r- read,w- write,x- execute.

Tip:Use the-hcommand line option along with-lto display file size in human readable format.

How to change file permissions?

Use thechmodcommand to alter file permissions.

There are two ways in which this command can be used.

Lettersa,o,u, andgrepresent all, others, owner, and group respectively.

The second method is thenumbers methodand it uses4,2, and1instead ofr,w, andx.

The values are added together in sets of 3 to give us a three digit number denoting permissions.

How to change file timestamps?

Use thetouchcommand to change file timestamps.

There are three types of timestamps associated with a file:Access time,Modification time, andChange time.

Similarly, the-moption changes the filemodification timeto the current time.

To change file timestamps to a time other than the current time, use the-tcommand line option.

Use the-dcommand line option for this.

How to determine file types?

Use thefilecommand to determine file types.

As shown in the example below, the command expects a filename as an argument.

I’ve downloaded an executable file, but it doesn’t execute, why?

Giving permissions manually also helps prevent virus, worms, and more from infecting your system without your knowledge.

How to print the number of new lines, words, and bytes in files?

Use thewccommand to print newline, word, and byte counts for a file.

How to display disk usage of files and directories?

Use theducommand to display disk usage of files and directories.

Here is an example:

Note- The-hcommand line option is used to produce the size in human readable format.

The reason behind this difference is eitherslack space or sparse files.

To display the amount of disk space available on thefile systemcontaining a specific file or directory use thedfcommand.

Here again, the-hoption is used to display the output in human readable format.

If thedfcommand is run without any file/directory name, it’ll show disk usage for all the file systems.

How to compare two files?

Use thediffcommand to compare two files.

The command requires two filenames as arguments, as shown in the example below.

How to view the first few and last few lines of a file?

Use theheadandtailcommands to quickly view the first and last few lines of a file.

These commands come in handy when you just want to have a quick peek inside the file.

For example, thehead -n2 somefilecommand displays the first2lines of the filesomefile.

Similarly, thetail -n3 somefilecommand displays the last3lines of the file.

How to store and view the output of a command at once?

How to compress and uncompress a file?

Working on Linux requires you to deal with archives like.tar,.tar.gz,.bz2, and more.

To create as well as uncompress these archives you might use thetarcommand.

For example, thetar -cvf practice.tar practice/command compresses thepracticefolder and creates a.tararchive namedpractice.tar.

To uncompress the.tararchive created above, use thetar -xvf practice.tarcommand.

The-xcommand line option signals the command to extract the archive.

This command untars the file in the current directory.

Use the-Coption to specify a different target directory.

To create.tar.gzand.tar.bz2archives, add an extra-zand-jcommand line option, respectively.

The command to uncompress these archives is same as the one used for.tarfiles.

Tip- To deal with.zipfiles, use thezipcommand.

How to edit a file using Vim editor?

But the basics of editing are simple and easy.

To open a file in the editor, run thevimcommand with the file name as an argument.

For example,vim textfile.

There are two operation modes in Vim:command modeandinsert mode.

To save the file, key in the:wcommand and then hitEnter.

To paste, presspin the command mode.

We’ve barely scratched the surface here, as the Linux command line has much to offer.

If you get stuck somewhere, leave us a comment or head over to ourSoftware & Apps forum.

Note:This feature was originally published in June 2014.

Part of our #ThrowbackThursday initiative.