Essential Linux Commands - Part 1

Essential Linux Commands - Part 1

Every User Must Know

Linux is an entire family of open-source Unix operating system that are based on the LINUX Kernel. This includes all of the most popular Linux based systems like Ubuntu, Fedora, Mint, Debian, and others. More accurately, they’re called distributions or distros.

When operating a Linux OS, you need to use a shell — an interface that gives you access to the operating system’s services. Most Linux distributions use a graphic user interface (GUI) as their shell, mainly to provide ease of use for their users.

It’s recommended to use a command-line interface (CLI) because it’s more powerful and effective. Tasks that require a multi-step process through GUI can be done in a matter of seconds by typing commands into the CLI.

So if you’re considering using Linux, learning basic command lines will go a long way. In this article, you’ll learn basic Linux commands that will undoubtedly help you navigate through Linux as a newbie.

1. pwd command

  • Use the pwd command to find out the path of the current working directory (folder) you’re in.
  • The command will return an absolute (full) path, which is basically a path of all the directories that starts with a forward slash (/).
  • An example of an absolute path is /home/username.

2. cd command

To navigate through the Linux files and directories, use the cd command. It requires either the full path or the name of the directory, depending on the current working directory that you’re in. There are some shortcuts to help you navigate quickly:

  1. cd .. (with two dots) to move one directory up
  2. cd to go straight to the home folder
  3. cd- (with a hyphen) to move to your previous directory

    On a side note, Linux’s shell is case sensitive. So, you have to type the name’s directory exactly as it is.

Let’s say you’re in /home/username/Documents:

  • want to go to Photos, a subdirectory of Documents then just type the command: cd Photos.
  • want to go in completely new directory, Ex. from username/Document to username/Movies in this scenario use directory's absolute path: cd/home/username/Movies.

3. ls command

The ls command is used to view the contents of a directory. By default, this command will display the contents of your current working directory.

If you want to see the content of other directories, type ls and then the directory’s path. For example, enter ls /home/username/Documents to view the content of Documents.

There are variations you can use with the ls command:

  • ls -R will list all the files in the sub-directories as well
  • ls -a will show the hidden files
  • ls -al will list the files and directories with detailed information like the permissions, size, owner, etc.

3. ls command

The ls command is used to view the contents of a directory. By default, this command will display the contents of your current working directory.

If you want to see the content of other directories, type ls and then the directory’s path. For example, enter ls /home/username/Documents to view the content of Documents.

There are variations you can use with the ls command:

  • ls -R will list all the files in the sub-directories as well
  • ls -a will show the hidden files
  • ls -al will list the files and directories with detailed information like the permissions, size, owner, etc.

4. cat command

cat (short for concatenate) is one of the most frequently used commands in Linux. It is used to list the contents of a file on the standard output (sdout). To run this command, type cat followed by the file’s name and its extension. For instance: cat file.txt.

Here are other ways to use the cat command:

  • cat > filename creates a new file
  • cat filename1 filename2>filename3 joins two files (1 and 2) and stores the output of them in a new file (3)
  • to convert a file to upper or lower case use, cat filename | tr a-z A-Z >output.txt

5. cp command

Use the cp command to copy files from the current directory to a different directory.

For instance, the command cp scenery.jpg /home/username/Pictures would create a copy of scenery.jpg (from your current directory) into the Pictures directory.

6. mv command

The primary use of the mv command is to move files, although it can also be used to rename files.

The arguments in mv are similar to the cp command. You need to type mv, the file’s name, and the destination’s directory. For example: mv file.txt /home/username/Documents.

To rename files, the Linux command is: mv oldname.ext newname.ext

7. mkdir command

Use mkdir command to make a new directory — if you type mkdir Music it will create a directory called Music.

There are extra mkdir commands as well:

To generate a new directory inside another directory, use this Linux basic command mkdir Music/Newfile

  • use the p (parents) option to create a directory in between two existing directories. For example, mkdir -p Music/C137/Newfile will create the new “C137” file.

8. rmdir command

If you need to delete a directory, use the rmdir command. However, rmdir only allows you to delete empty directories.

9. rm command

The rm command is used to delete directories and the contents within them. If you only want to delete the directory — as an alternative to rmdir — use rm -r.

Note: Be very careful with this command and double-check which directory you are in. This will delete everything and there is no undo.

10. touch command

The touch command allows you to create a blank new file through the Linux command line. As an example, enter touch /home/username/Documents/Web.html to create an HTML file entitled Web under the Documents directory.

11. locate command

You can use this command to locate a file, just like the search command in Windows. What’s more, using the -i argument along with this command will make it case-insensitive, so you can search for a file even if you don’t remember its exact name.

To search for a file that contains two or more words, use an asterisk (*). For example, locate -i school*note command will search for any file that contains the word “school” and “note”, whether it is uppercase or lowercase.

12. find command

Similar to the locate command, using find also searches for files and directories. The difference is, you use the find command to locate files within a given directory.

When using the find are:

  • To find files in the current directory use, find . -name notes.txt
  • To look for directories use, / -type d -name notes. txt

===================================================

Ok! This post is getting really long.

Let me name this post as, Linux Basic Commands part 1 and I will continue in part 2 from this point.

Stay tuned and keep going crazy. Yeehaw🔥

#2Articles1Week #Hashnode