4 min read

Top 15 Linux Commands for Web Developers

Below is a quick summary of basic Linux commands. If you're not that familiar with Linux commands, this short cheat-sheet may be useful to you.

1. pwd

Prints the current working directory.

Lots of terminals show the current path in the prompt (set by the PS1 environment variable), however it might happen that they don't according to their respective settings — you can use the pwd command in such case.

$ pwd
/home/r
$

2. ls

Lists the directory's contents.

If no parameter is supplied, then it'll list the current directory.

$ ls
webapp
$

You can also specify the target folder you want to list:

$ ls /home
r
$

By default it shows only the name of the files and folders. Adding options -lart will provide more information, such as:

  • l - prints the files and directories line by line (long listing format)
  • a - also shows directories and files starting with .
  • t - sorts by the item's last modification timestamp
  • r - reverses the sorting order, that is, the most recent files and directories will appear last when combined with the previous option

ls with additional parameters

3. cd

Changes the working directory.

When no parameter is provided, it'll change to the user's home directory. (Basically this is the equivalent of the cd $HOME command.)

Otherwise, it'll switch to the directory provided in the first parameter.

$ cd /home/r
$ pwd
/home/r
$

cd .. changes to the parent directory.

cd - changes to the previous directory. This is really cool if you want to quickly switch back to the previous folder which is not parent of the current one.

$ cd /some/very/long/path
$ cd /home
$ pwd
/home/r
$ cd -
$ pwd
/some/very/long/path

4. cat

Prints the file's content to the standard output.

cat

A relatively commonly used parameter for cat is -n which prints the line numbers as well:

$ cat -n index.php
     1  <?php
     2  echo "Hello World!<br>";
     3  echo date("Y/m/d");
$

5. diff

Provides a diff between two files.

diff

6. grep

Prints lines from the input matching the provided pattern.

This command is typically used in conjunction with cat. That is, we first read the file with with cat then we redirect its content to grep with the | (pipe) operator.

In the example below we look for the date word in the index2.php file:

grep with cat

7. touch

Updates the file's last modification timestamp or creates an empty file if it doesn't exist:

touch

8. cp

Copies files and directories.

Syntax:

cp [options] source destination

cp

If you want to copy a directory, you should use the -r (recursive flag).

One important note: by default, cp does not preserve the files' timestamp and permission information. Use the -rp options to keep these attributes.

9. mkdir

Creates a directory.

Syntax:

mkdir [options] directory

Sometimes it's really convinient to creat nested folders. However, by default, it's not allowed:

$ mkdir hello/world/foo/bar
mkdir: cannot create directory ‘hello/world/foo/bar’: No such file or directory
$

Using the -p flag will create the parent directories.

$ mkdir -p hello/world/foo/bar
$

10. mv

Moves or renames files and directories.

Syntax:

mv [options] source target

mv

11. rm

Removes files and directories.

Syntax:

rm [options] (file|directory)

Use the -r option to delete a directory.

12. ln

Creates a link.

Syntax:

content_copy

ln [option] source link

A commonly used option is -s which refers to symbolic link. (By default, it'll create a hard link.)

13. chmod

Changes the file's or directories' access permission.

Syntax:

chmod [options] mode (file|directory)

The mode can be represented by three digits. The first digit determines the access level for the file owner, the second digit is for the group, while the third one is for everyone else.

Each digit individually represents the following permissions:

  • 0 - Executable
  • 1 - Writable
  • 2 - Executable and Writable
  • 4 - Readable
  • 6 - Writable and Readable
  • 7 - Writable, Readable and Executable

On a Linux system, the default mode is set to 644. This means the following:

  • The owner of the file can read or write it
  • The group of the file can read it
  • Others can read it

chmod

14. tar

Creates and extracts archives.

Creating an archive:

$ tar czvf webapp.tar.gz webapp
webapp/
webapp/files/
webapp/index.php
webapp/index2.php
webapp/jquery-3.4.1.js
$

Extracting an archive:

$ tar xvf webapp.tar.gz
webapp/
webapp/files/
webapp/index.php
webapp/index2.php
webapp/jquery-3.4.1.js
$

15. zip

Creates and extract zip files.

Creating a zip archive:

$ zip -r webapp.zip webapp
updating: webapp/ (stored 0%)
updating: webapp/files/ (stored 0%)
updating: webapp/index.php (deflated 4%)
updating: webapp/index2.php (deflated 4%)
  adding: webapp/jquery-3.4.1.js (deflated 70%)
$

Extracting a zip archive:

$ unzip webapp.zip
Archive:  webapp.zip
   creating: webapp/
   creating: webapp/files/
  inflating: webapp/index.php
  inflating: webapp/index2.php
  inflating: webapp/jquery-3.4.1.js