20 Useful Linux Commands For Developers

20 Useful Linux Commands For Developers

Introduction

Hi friends, welcome back to my blog! If you’re reading this post, you came to learn 20 Linux commands that'll help you along your programming journey. But before I introduce you to my list, I must tell you what Linux is.

What is Linux?

Linux is a family of open source Unix operating systems based on the Linux kernel that serves as an alternative to operating systems like Windows and Mac OS. Linux contains popular distributions, or operating systems like Ubuntu, Mint, Debian and many others.

Although there are many differences among Linux and other operating systems, there are some similarities. Out of all the similarities that I could include, there is one common occurrence in many operating systems: a command prompt.

What is a command prompt?

A command prompt is a command line interpreter used to execute commands in a operating system. It is also known as a shell. There are many reasons why you would use a command prompt such as to create files and navigate through directories.

To interact with the command prompt, a user must enter specific commands in the shell. Without any delay, here is a list of 20 useful Linux commands for developers.

Linux Commands

1) pwd

Pwd, or present working directory, is a command you can use to find out what directory you are currently working in. Simply type in "pwd" in your shell, press the "Enter" key and you'll see which directory that you are currently working in.

$pwd

2) man

Man, short for manual, sounds exactly like what it's used for. It's a command used to search for how to use commands.

SYNTAX: $man [COMMAND NAME]

$man cd

If you want to learn how to use the "man" command, you can simply type "man man" and hit the "Enter" key in the shell. Look below to see how it looks:

$man man

3) exit

Use this command if you want to exit your shell. Simply type "exit," press the "Enter" key and you'll immediately be exited from the command line interface.

4) mkdir

Use this command to create new directories. Just to note, you can only use this command if you want to create new directories. If you try to create a directory that exists, you'll receive a message stating that you can't create that directory because the file already exists.

SYNTAX: mkdir [DIRECTORY]

$mkdir bin

5) ls

Use this command to list the content of a directory. You can simply type "ls" and press the "Enter" key to see the files in your current working directory. One important thing you can utilize with this command is a flag.

A flag is a set of options you can pass through a command argument. Flags are typically designated with letters. For example, if you want to list all hidden files of a directory, you can type "ls -a" and hit the "Enter" key. By default, your command line will only show you files that don't start with a period but you can see all of your files utilizing the "-a" flag.

SYNTAX: ls [FILE|DIRECTORY]

$ls bin

6) file

Use this command if you want to know the type of a specific file. This is another useful command specifically in Linux since file extensions like ".zip" ".txt" and ".csv" are indistinguishable from each other.

SYNTAX: file [FILENAME]

$file grime.txt

7) rm

Use this command if you want to remove files. If you want to remove multiple files, simply add them to your argument. If you want to remove empty directories, you can use the "-d" flag.

If you want to remove non-empty directories, you can use the "-r" flag. Simply type "-rm -r [DIRECTORY]" and press the "Enter" key to remove the non-empty directory.

You can also use the "rmdir" command to remove empty directories.

SYNTAX: rm [FILE...]

$rm blank.txt

8) cd

Use this command if you would like to change the shell's working directory. If you don't add a directory, the default value will be your home directory.

You can also type "cd ~" and press the "Enter" key to go to your home directory. If you want to go to a parent directory, which is the directory directly above another directory, simply type "cd .." and press the "Enter" key.

SYNTAX: cd [DIRECTORY]

$cd src

9) mv

Use this command to move or rename files and directories. First, type the command "mv," then type the file or directory that you would like to move or rename. Lastly, type the name of the file or directory that you would like to rename it to or move your initial file into.

SYNTAX: mv [SOURCE][DESTINATION]

$mv a.txt b.txt

10) touch

Use this command to create a new file.

SYNTAX: touch [FILE]

$touch c.txt

11) cp

Use this command to copy files and directories. While the "cp" command requires at least two filenames in its arguments, you can also use multiple files in your argument. First, type the command "cp," then type the file or directory that you would like to copy. Lastly, type the name of the file or directory that you would like to copy the contents of the initial file or directory into.

If you want to copy a directory, meaning its content, to a new directory, simply use the "-r" flag to copy the entire directory structure recursively.

$cp -r /Documents /new_directory

The "cp" command is similar to the "mv" command, however when you use the "mv" command to move one file to another destination, the original file will be deleted. When you use the "cp" command, the original file and the copied file will remain as is.

SYNTAX: mv [SOURCE][DESTINATION]

$cp j.txt k.txt

12) ln

Use this command to create a link between files. A link is just a shortcut to an original directory. There are many uses to creating links between files including keeping copies of a file in multiple locations or to create a easy path to a directory within a crowded file hierarchy. Moreover, there are two basic types of links: a hard and soft link.

A hard link allows you to link to only files, not directories. By creating a hard link between two files, the contents of both files exist in two places. If you delete the target file, the shortcut will remain unchanged.

SYNTAX: ln [TARGET][LINK_NAME]

$ln demo2 floppy2

A soft link , also known as a symbolic link, can link to files or directories across file systems and acts as a shortcut to another location in your file system. However, unlike regular links, once the original file is deleted, the symbolic link will break.

SYNTAX: ln -s [TARGET][LINK_NAME]

$ln -s demo1 floppy3

13) cat

Use this command to display the contents of files and combine them into one file. The "cat" command doesn't accept directories as an argument.

SYNTAX: cat [FILE_1][FILE_2...]

$cat f.txt g.txt

If you look above, using the following syntax will list the content of both files in your shell. However, to combine the content of both files into one file, you must use something called an output redirection operator.

An output redirection operator is used to the send the output of a command to a file. This operator is denoted by using a greater than symbol(>).

$cat jabber wocky > bike

When using the output redirection operator in the example above, the content of the files "jabber" and "wocky" will be combined and added to a new file called "bike." If the target file exists, it will be overwritten. However, if you don't want to overwrite a file, but simply add content to that specific file, use the ">>" operator.

$cat jabber wocky >> bike

In the example above, the "jabber" and "wocky" files will be combined and added below the content of the "bike" file.

14) echo

Use this command to print a string of text in the shell. Just to note, it's good practice to put your string of text in quotation marks.

SYNTAX: echo [STRING]

$echo "Hello, World!"

You may ask, "What's the point of just printing a string of text to a screen?" However, the echo command is very useful. One of the main uses of the "echo" command is to add content to files.

For example, you can use the "echo" command and ">>" operator to add content to a text file. In the example below, the command prompt will take the output of "echo champ" and add it below the content of the "c.txt" file.

$echo champ >> c.txt

15) df

Use this command to see the disk space of your file systems.

SYNTAX: df [FILE]

$df

Some useful flags to use with the "df" command is the "-h" and "-x" flag. The "-h" flag allows you to see file sizes in a human-readable format. It will display the file size in Megabytes or Gigabytes rather than bytes.

The "-x" flag, or exclude option, limits the listing of certain file systems. Sometimes, you'll want to see a particular file system rather than a lot of filesystems on your screen.

$df -x udev

Conversely, if you want a tailored list of file types, you can use the "-t" flag.

$df -t tmpfs

16) chmod

Use this command to change file permissions.

SYNTAX: chmod [REFERENCE][OPERATOR][MODE] FILE

If you look at the example below, you should notice nine characters at the beginning of the output starting with "r."

$ls -l /home/benji
-rw-r--rx- benji user 438 Nov 30 1:24 panda
-rw-r--rx- benji user 439 Nov 30 1:30 koala

The command "chmod" is also known as "change mode" because the nine characters are known as the security "mode" of the file. Here are what the letters symbolize:

  • r: you can "read" or view the file's comments
  • w: you can "write" or modify the files contents
  • x: you can "execute" the file, only if it's a program (e.g. not a text file)
  • if any of the "rwx" characters is replaced by a "-," then that permission has been revoked

The nine characters of the security mode are divided into three separate parts. Here are those parts below:

  • owner (benji): read and write
  • group (user): read
  • other: read and execute

The easiest way to learn how to modify the file permissions is to use the symbolic method. While using the symbolic mode, you can use operators to change file permissions. Here are the operators you can use below:

  • +: adds the chosen permission(s) to the file or directory.
  • -: removes the chosen permission(s) to the file or directory.
  • =: sets the chosen permission(s).

Let's say you want to give your group the permission to write in the "panda" file. You can use the "who, what, which" method to help you.

who: who are we setting the permission(s) for?

  • u: user
  • g: group
  • o: others
  • a: all, meaning all of the above

what: what change(s) are we making?

  • -: removes the permission
  • +: grants the permission
  • =: set a permission and remove others

which: which permission(s) are we setting?

  • r: read
  • w: write
  • x: execute
$chmod g + w panda

In the example above, the group received the "write" permission for the file "panda." The example below shows you how you can change multiple file permissions at once.

$chmod a - rwx panda

17) grep

Use this command to search for a string of characters in a specific file.

SYNTAX: grep PATTERNS [FILE]

$grep level paper

In the example above, the shell will output every line of text in the file "paper" that contained the string of text "level."

18) find

Use this command to search for specific files in a directory hierarchy.

SYNTAX: find [STARTING POINT][EXPRESSION]

Let's say you want to search for a file with a specific name. Look at the example below to find out how to do this:

$find ~ -name "balloon"

The example above shows that you want to search for a file named "balloon" with the "-name" option in your home directory(~).

If you want to search for a file with a particular file pattern, look at the example below.

$file ~ -name "*.txt"

By using the star symbol, also known as a wildcard, next to the file pattern in quotation marks, the output of the example above will specify all files that have a ".txt" at the end.

19) diff

Use this command to find the difference between two files. This command will show you the differences between files line by line.

SYNTAX: [FILE1][FILE2]

$diff y.txt z.txt

20) history

Use this command to list all the previous commands you've used. While working in the Linux shell, it saves a history of commands you've already ran, and you can search them. You simply type in "history" in your shell to view your previously used commands. The output will list the command you used and the command number.

$history

One clever thing about the "history" command is that it can actually save time and boost your productivity. For example, you can repeat a previous command you've used by using an exclamation point(!) and the number of the command.

$history !43

If you want to delete commands from your history list, you can use the "-d" option, along with the command number.

$history -d 43

If you want to learn some other Linux commands, here are some more .

Conclusion

Thanks for reading my article! If you enjoyed it, don't forget to give it a like and share.👍🙏

If you have any questions, this is the quickest place to reach me .

Follow me on 👉👉👉

✨Twitter: @kahsmit

✨LinkedIn: @kahlei smith

✨GitHub: @mrkoderr

Finally, if you haven't already, follow me on Hashnode if you're interested in topics like

-🌐Web development (HTML, CSS, JavaScript etc.)

-🔗Blockchain

-🖥Crypto

-💎Web 3.0

-⚡all things tech

If you're not already on Hashnode, you can create an account here. 👈👈👈