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
File System
List files and dirs ls, ls- l (list with more info), ls -la (list all including hidden files)
Make a file touch <the-new-file-name>
Remove a file rm <the-file-name>
Make a directory mkdir <the-new-directory-name>
Remove dir and all under it rm -rf <the-directory-name>
Copy a directory cp -r <the-source-file-or-directory-name> <the-file-or-directory-copy-name>
Change owner sudo chown <the-user-name> <the-file-or-directory-name>
Change user sudo chgrp <the-group-name> <the-file-or-directory-name>
Find a file find <the-path-to-search-in-or . for-current-dir> -name <the-file-name>
Find a directory find <the-path-to-search-in . for-current-dir> -type d -name '<the-directory-name>'
List search results ls <the-dir-path> grep <the-string-to-search-for>
Page long output <my_command> | less (use space bar to page, ctrl+c to exit)
Apps Services and Processes
List all running processes ps -ef (all processes), ps (running processes)
Remote File System
Login to a remote server ssh <the-username>@<the-host-name>
Copy file from a server scp <the-username>@<the-host-name>:/path/to/server/file /path/to/local/dir/
Copy dir from a server scp -r <the-username>@<the-host-name>:/path/to/server/dir /path/to/local/dir/
Copy file to a server scp /path/to/local/file <the-username>@<the-host-name>:/path/to/server/dir
Copy file between servers scp username@remote_1:/file/to/send username@remote_2:/where/to/put
Permissions (flags)
There are four OCTAL (0..7) digits, which control the file permissions. But often, only three are used. If you use 600 it equals 0600. The missing digit is appended at the beginning of the number.
Each of three digits described permissions. Position in the number defines to which group permissions do apply!
Permissions:
1 – can execute
2 – can write
4 – can read
The octal number is the sum of those three permissions, i.e.
3 (1+2) – can execute and write
6 (2+4) – can write and read
Position of the digit in value:
1 – what owner can
2 – what users in the file group(class) can
3 – what users not in the file group(class) can
Examples:
chmod 600 file – owner can read and write
chmod 700 file – owner can read, write and execute
chmod 666 file – all can read and write
chmod 777 file – all can read, write and execute
Host
hostname hostname
FQDN hostname -f
IP Address hostname -I
OS lsb_release -da
Network Look-Up nslookup portal-dev.ral.ucar.edu
Apache
Apache Version /usr/sbin/apache2ctl -v
Apache status sudo systemctl status apache2
Stop Apache sudo systemctl stop apache2
Start Apache sudo systemctl start apache2
Restart Apache sudo systemctl restart apache2
PHP
PHP Version php -v
php.ini php -i | grep php.ini
PHP Binary Location which php
Git
clone repo to current dir git clone git@github.com:NCAR/nrit-web-visit-app.git
Perl
Perl Version perl -v
MySQL
MySQL Version mysql -V
Login mysql -u root -p
Restart mysql sudo /etc/init.d/mysql restart
Remote Command Line Login
In order to access a DB you must use the mysql
client. If it is not installed on your local machine, you can ssh into another host that does have it, like ral-itapps-a
, and then login to the DB.
mysql -u <the-sql-user> -p'<the-sql-password>' \
-h <the-database-host-url> -P 3306 \
-D <the-database-name>
MySQL Commands
Show the current user SELECT USER();
Show all users SELECT DISTINCT User FROM mysql.user;
Show user privileges SHOW GRANTS FOR 'my_user'@localhost;
Create a user CREATE USER 'my_user'@localhost IDENTIFIED BY 'my_users_password';
Delete a user DROP USER 'my_user'@localhost;
User Privs https://phoenixnap.com/kb/how-to-create-mariadb-user-grant-privileges
Show all databases SHOW DATABASES;
Create a new database CREATE DATABASE 'my_db_name';
Use a database USE my_db_name;
Show all tables SHOW tables;
Show table fields DESCRIBE my_table_name;
Show table full fields SHOW FULL COLUMNS FROM my_table_name;
Host Name SHOW VARIABLES LIKE '%host%';
Port SHOW VARIABLES LIKE '%port%';
Character Sets SHOW VARIABLES LIKE '%character_set%';
Collations SHOW VARIABLES LIKE '%collation%';
SQL Mode SHOW VARIABLES LIKE '%sql_mode%';
Socket SHOW VARIABLES LIKE '%socket%';
DB Data Path SHOW VARIABLES LIKE '%datadir%';
Engine Type SHOW VARIABLES LIKE '%storage_engine%';
Error Log Path SHOW VARIABLES LIKE '%log_error%';
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
- For more info see: https://askubuntu.com/questions/725120/can-anybody-explain-output-of-getent-passwd-command
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 |