Essentials

General

Command

Description

echo "TEXT"

Output any text "TEXT" provided

whoami

Find out what user we're currently logged in as

man man

Shows info about man command and lists all the sections of the manual

strace objFile

Lists all the system calls used in the program

ltrace objFile

Lists all the library calls used in the program

gcc -o objFile source -v

Lists all the path of libraries and headers used in creation of objFile

du -sh folder

Folder size

df -h

Disk space usage

free -h

Memory usage

uname -a

Kernel and system info

uptime

System uptime

hostname

Hostname

Network commands

Command

Description

netstat -t

Lists all the active TCP connections showing domain names.

netstat -u

Lists all the active UDP connections showing domain names.

netstat -n

Lists all the active, showing IP and port numbers.<

nslookup domain

Shows the IP address related to the domain (E.g. IP of www.google.it)

dig @server name type

DNS lookup utility:

  • server name or IP address of the name server to query;

  • name name of the resource record that is to be looked up;

  • type type of query is required (ANY, A, MX, SIG, etc.). If no type is specified, A is performed by default.

wc [file]

Prints in order newlines, words, and bytes (characters) counts for file. If file not specified or equal to -, counts from stdin.

route -n

Show numerical addresses instead of trying to determine symbolic hostnames in routing table.

arp -a

List all the MAC addresses stored after some ARP requests and replies made by our ethernet interfaces.

ifconfig

List network interfaces.

Search strings in files

Option

Description

-e pattern

Specify pattern

-i

Case insensitive

-v

Invert match (List all string without matches)

-c

Output count of matching lines only

-l

Output matching files only

-n

Precede each matching line with a line number

-b

A historical curiosity: precede each matching line with a block number

-h

Output matching lines without preceding them by file names

-s

Suppress error messages about nonexistent or unreadable files

-w

Force PATTERN to match only whole words

-x

Force PATTERN to match only whole lines

-f file

Take regexes from a file

-o

Output the matched parts of a matching line

Files and directory management

Option

Description

cd

Take regexes from a file

cp X PATH

Make a copy of file/folder X into the path PATH

mv X PATH

Move/rename the file/folder X in PATH

cat X

Concatenate file content of X

pwd

Print working directory

touch FILE

Create a file with name FILE

rm FILE

Delete a file with name FILE

mkdir DIR

Create a directory with name DIR

rmdir DIR

Delete an empty directory with name DIR

rm -r DIR

Delete recursively a directory with name DIR

Process management

Option

Description

CMD &

If specified at the end of the command, run it in background.

CMD1 && CMD2

Combine multiple commands together in one line of your terminal.

CMD1; CMD2

Execute several command in order (one after the other).

Option

Description

ps -f

See background processes

ps -e

More info about processes

ps -a

Select all processes except both session leaders and processes not associated with a terminal

ps -u USERLIST

Select by effective user ID (EUID) or name

fg PID

It continues the stopped job with id PID by running it in the foreground

bg

A job control command that resumes suspended jobs (CTRL+Z)

kill PID

It terminates the job with id PID

kill -9 PID

It forces kill of the job with id PID

top

It shows all the processes running in Linux working environment

htop

It shows all the processes running in Linux working environment

UNIX common Files

File path

Description

/etc/hosts

Local resolution table.

/etc/services

List all the applications with their port

/etc/protocols

Internet protocols.

/usr/include/x86\_64-linux-gnu/bits/socket.h

List all the protocol type possible for socket.

/usr/include/x86\_64-linux-gnu/sys/socket.h

Definition of struct sockaddr and specific ones.

Shell operators

Operator

Description

CMD1 | CMD2

Redirect command output to input of the next command

!!

Previous launched command

CMD2 < CMD1

Redirection of the command input (overwriting text)

CMD1 > CMD2

Redirection of the command output (overwriting text)

CMD1 >> CMD2

Redirection of the of the command output (appending text)

Download files

Command

Description

wget URL

Download to the current folder

wget -O <new_filename> URL

Download with a different name

wget -c URL

Resume download

curl -O URL

Download to the current folder

curl -o <new_filename> URL

Download with a different name

curl -C - -O URL

Resume download

SCP

SFTP

SMB

Then, inside the prompt:

Transfer files

Transfer a file from the SOURCE path to the DEST path, using SSH channel:

SOURCE and DEST have the following format:

or

Automation

  • Open the crontab by running the following command:

A crontab is simply a special file with formatting that is recognised by the cron process to execute each line step-by-step.\ The crontab will execute each line as a cronjob.\

Crontab line fomat

Each line of the crontab has the following format:

Keyword

Description

MIN

What minute to execute at

HOUR

What hour to execute at

DOM

What day of the month to execute at

MON

What month of the year to execute at

DOW

What day of the week to execute at

cmd

The actual command that will be executed

Special time interval statements

Statement

Description

@yearly

Run the task every year at 12:00am on the 1st of January

@annually

"

@daily

Run the cronjob every day at 12:00am

@midnight

"

@monthly

Run the job once a month, on the 1st, at 12:00am

@weekly

Run the job once a week at 12:00am on Sunday

@hourly

Run the job at the top of every hour

@reboot

Run the task once, at startup

Open crontab with different user

Time management

Current time

Available timezone

Set new timezone

Users management

  • Create the user username

Option

Description

-m

  • Creates the home directory for the user (by default /home/username)

  • Copies default files from /etc/skel (like .bashrc, .profile)

  • Sets correct ownership and permissions

-G <group>

Automatically add the user to the group <group> (the group must exists before the execution)

-m -d <new_home_path>

Creates the home directory for the user at <new_home_path>

If -m is not specified, the user <username> is created without home directory.

  • Set a password for the user username

  • Change user

  • Delete a user

  • Get user id

  • Get user's Groups

Groups management

  • Create a group

Option

Description

-g <gid>

Create the group with a specific group ID <gid>

  • Add users to a group

  • Delete a group

Permissions

List file and directions

Command

Description

ls -a

List all files, including hidden ones (filename starting with ".")

ls -color

Colored list [=always/never/auto]

ls -l

List with long format (show permissions)

ls -h

List with readable file size

ls -r

List in reverse order

ls -R

List recursively directory tree

ls -s

List file size

ls -S

Sort by file size

ls -t

Sort by time & date

ls -X

Sort by extension name

Permission
Directory meaning

r

list contents

w

create/delete files

x

enter/access directory

Change permissions

If -R specified with chmod on a folder, the operation is applied recursively.

Examples:

chmod permission options

Targets (who)

Symbol
Meaning

u

user / owner

g

group

o

others

a

all (u+g+o)

Operators

Symbol
Meaning

+

add permission

-

remove permission

=

set exactly

Permissions

Symbol
Meaning

r

read

w

write

x

execute

Common combinations

Command
Effect

chmod u+x file

add execute for user

chmod g+x file

add execute for group

chmod o+x file

add execute for others

chmod a+x file or chmod +x file

add execute for all

chmod ug+x file

add execute for user & group

chmod go+x file

add execute for group & others

chmod u-x file

remove execute from user

chmod g-x file

remove execute from group

chmod o-x file

remove execute from others

chmod a-x file

remove execute from all

Set exact permissions (=)

Command
Result

chmod u=x file

user: execute only

chmod g=rx file

group: read + execute

chmod o= file

others: no permissions

chmod a=rx file

all: read + execute

Multiple permissions at once

Command
Effect

chmod u+rwx file

full access for user

chmod g+rx file

read & execute for group

chmod o+r file

read only for others

File/Folder ownership

Common chown usages

Command
Effect

chown user file

change owner only

chown user:group file

change owner and group

chown :group file

change group only

chown user: file

change owner, keep group

chown user.group file

same as user:group

Useful options

Option
Meaning

-R

recursive

-v

verbose (show changes)

-c

report only when changes occur

--reference=<file_path>

copy owner/group from another file

Last updated