Linux Commands

About Commands

Parameters and Flags

  • A single hyphen (-) can be followed by multiple single-character flags.
  • A double hyphen (--) prefixes a single, multi-character option.

Consider this example:

tar -czf

In this example, -czf specifies three single-character flags: c, z, and f.

Now consider another example:

tar --exclude

In this case, --exclude specifies a single, multi-character option named exclude. The double hyphen disambiguates the command-line argument, ensuring that tar interprets it as exclude rather than a combination of e, x, c, l, u, d, and e.

Commands

chmod – Change Mode

The chmod command sets the permissions of files or directories.

chmod <options> <permissions> <file name>
sudo chmod -R 755 /var/www/my_website

See the Linux chmod page for more information.

chown – Change Ownership

Used to change the ownership of a file or directory to another user and or group.

sudo chown -R www-data: /path/to/directory

find – Find Files

Syntax:

find <starting path> <options> <file name>

Find a file starting at the / path, non-case sensitive, for the name linux.odt:

find / -iname linux.odt

See the Linux find page for more information.

getent – Get Entries

getent is a Unix command that helps a user get entries in a number of important text files called databases. This includes the passwd and group databases which store user information – hence getent is a common way to look up user details on Unix.

Example: Get all the entries of the /etc/passwd file:

getent passwd

Outputs something like:

root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
dwatts:x:34182:1500:Daniel Watts:/home/dwatts:/bin/bash

grep – Search File Content

grep is a string and pattern matching utility that displays matching lines found in files, and can also work with piped output from other commands.

Find all instances of the string my string in the files under the /path/to/search directory:

grep -r "my string" /path/to/search

See the grep command page for more information.

ls – List

List the names and information about files and directories.

Passing the -la flag lists ALL files and directories (including hidden files).

$ ls -la

total 40
drwxr-xr-x  7 root  staff   224 Apr 27 10:41 .
drwxr-xr-x  7 root  staff   224 Jan 22 15:48 ..
drwxr-xr-x  8 root  staff   256 Jan 22 15:48 .svn
-rw-r--r--  1 root  staff  1897 Apr 27 10:41 email.php
-rw-r--r--  1 root  staff  2695 Jan 22 15:48 file_downloads.php
-rw-r--r--  1 root  staff  6373 Apr 28 09:35 handle_errors.php
-rw-r--r--  1 root  staff  1191 Jan 22 15:48 mime_types.php

ls -l Output Columns

1 - File Permissions
│           2 - Number of hard links
│           │ 3 - Owner name
│           │ │              4 - Group Name
│           │ │              │      5 - File Size
│           │ │              │      │    6 - Month of last modification
│           │ │              │      │    │   7 - Day of last modification
│           │ │              │      │    │   │  8 - Time of last modification
│           │ │              │      │    │   │  │     9 - File or Directory Name
│           │ │              │      │    │   │  │     │
└─────────  └ └────────────  └────  └─── └── └─ └──── └────────  
-rw-r--r--  1 squarem1admin  staff  1897 Apr 27 10:41 email.php

mkdir – Make Directory

Use the -p flag to also create the any parent directories.

mkdir -p /path/to/directory

nano – File Editor

Create a new file and open it for editing with nano:

nano /path/to/file.ext

Use control+z to exit and save.

rm – Remove

Remove will remove a directory or a file.
Passing -r will remove everything below (inside) the directory you are removing.

rm -r directory_to_remove

tail – Read the Tail of a File

The tail command reads a file, and outputs the last part of it (the “tail”).

umask – Mode Creation Mask

The umask command returns, or sets, the value of the system’s file mode creation mask.

Web Server Commands

Linux Host

Description Command
Get the current operating system. lsb_release -da
Get the hostname of the current machine. hostname
Get the IP Address of the current machine. hostname -I
Get the FQDN of the current machine. hostname -f

MySQL Database Server

Description Command
MySQL version mysql -V

Apache HTTP Server

Description Commands
Apache server version /usr/sbin/apache2ctl -v

PHP

Resource Name Command
PHP Version php -v
PHP Configs (php.ini) Location php -i | grep php.ini
PHP Location which php