Linux administration (UNIX Terminal)

Home

+-------------------------------------------------------------------------------+
|   man                                                                         |
|   short for manual, it is a documentation usually found on a UNIX system.     |
+-------------------------------------------------------------------------------+

Format

man <commande>

Example:

man ls

RQ: RTFM means Read The Fucking Man

Commands

Various

Add a message of the day at login

vi /etc/motd
Your message

Concatenate two file in one

cat file1 file2 > finalFile

Create new directory

Format

mkdir path/dirname

Examples:

mkdir myDir
mkdir /home/username/Desktop/myNewDir

Create new file

Format

touch path/filename

Examples:

touch README
touch /home/username/Desktop/script.py

Determine file type

file filename

For multiple files, separate them with spaces.

file file1 /dir/file2

Display 'Hello' and the current date

echo "Hello " $(date '+%A %W %Y %X')

Execute a command as root

su root -c "nano /etc/fstab"

Find filenames quickly (needs updateDB)

locate filename
updatedb

To be able to make a search, you have to build a database of your filesystem.

The norm is to execute updatedb in a cron: cron.daily at5 am. (see cron)

updatedb

Get a calendar of the current month

cal

Get current directory

pwd

Get current user

whoami

Get the date

date

Get last logins of users and ttys

last

Get user's terminal name

Format

tty

Example:

tty
/dev/ttys000

Get line, word, character, and byte count of a file

Displays, by default, the number of lines, words, and bytes.

wc file

Example:

cat test.txt
This is a short line.
This is a second short line.
wc test.txt
       2      11      51 test.txt

Locate a program file in the user's path - which

Format

which program

Example:

which passwd
/usr/bin/passwd

Move in file like in Vim - less

less myFile.txt

Move in command result like in Vim - less

chkconfig | less

Search in command result - grep

chkconfig | grep sshd
#Search for sshd from listed services by chkconfig

Backup - tar

Save all the files (and dir) in 'file.tar.gz' and compress the data

tar -zcvf file.tar.gz files /dir

Save all the files and /dir in 'file.tar'

tar -cvf file.tar files /dir

Extract all the files and/or dir from the file (extract it from the root dir)

tar -xvf file.tar

Extract all the data from the dir inside the .tar

tar -xvf file.tar -C /dir

List all the content of the .tar

tar -tvf file.tar

Decompress the archive.

The second line is the result of ls.

gunzip file.tar.gz
file.tar

Clear the Terminal history

By pressing the up arrow in the command line we'll see the last command we ran. Keep pressing up and we’ll see more commands; we can go back days, months, or even years.

This is called the history, and it’s very convenient. If we made a mistake typing a long command, simply press up and fix the problem. If we want to re-connect to an SSH server we used the other day, simply press up until we see the relevant command.

It’s useful, but there’s also a potential security problem here, particularly if we accidentally typed a password in plain text at some point. How could we clear the history?

Here’s how to do it.

Clear all of your Bash history

If we want to remove the entirety of the history, we have to type this command:
rm ~/.bash_history.

Alternatively, we could open the file and delete any lines we want to.

Clear the current session’s history

The history can be broke down into two parts. There’s the current sessions’ history, and the long-term history.

Our first command deals with the current session: history -c.

The history command is built into Bash itself, and the -c modifier tells the program to clear that history.

This command will prevent anything in the current session from being written to the long-term history, but does not clear out that long-term history.

Compression - (g)zip

Compress the file with gzip into 'file.txt.gz'

gzip file.txt

Decompress the file

gunzip file.txt.gz

Display information about the file's compressed and uncompressed size

Also shows ratio, uncompressed name.

Add -v for more infos.

gzip -l file.ext.gz

Compress the files with zip into file.zip

zip file.zip files

Compress all the files and dirs from /dir

zip -r file.zip /dir

Compress multiple files and dirs

You have to separate them with spaces.

zip -r files.zip file1 file2 /dir

Decompress the file

unzip file.zip

Copy files

Copies the file and renames it

cp fileName newFileName

Copies the file in myDir/ (without renaming it)

cp fileName myDir/

Copies the file in myDir and renames it

cp fileName myDir/newFileName

Copies myDir with a new name

cp -R myDir myNewDir

Copies all the .jpg files into myDir

cp *.jpg myDir/

Disk usage - du

Get the disk usage for all the files of the disk

du

Get size of a file (or the files of a folder)

Default size (in units of 1024 bytes)

Format

du filename

Example:

du Desktop/Test/Grid.java
16      Desktop/Test/Grid.java
Human-readable output -h

Use unit suffixes: Byte, Kilobyte, Megabyte, Gigabyte, Terabyte and Petabyte.

Format

du -h filename

Example:

du -h Desktop/Test/Grid.java
8,0K    Desktop/Test/Grid.java

Get size of a folder - sh

Format

du -sh myDir/

Example:

du -sh Desktop/Test
22M Desktop/Test

Find

Find a file (locate is better)

Be careful Without -name, the command will return that the file doesn't exist.

In a given directory
find / -name filename
#The file will be searched everywhere → '/'.
In the current directory
find -name filename

Find a file from its last modified date

Files modified n days ago
find / -mtime n
Files modified more than n days ago
find / -mtime +n
Files modified less than n days ago
find / -mtime -n

List directory contents - ls

Lists current directory contents

ls

List in long format

Total sum for all the file sizes is output on a line before the long listing.

ls -l

Include hidden directory entries

Those whose names begin with a dot (.)

ls -a

Reverse the order of the sort

To get reverse lexicographical order or the oldest entries first (or largest files last, if combined with sort by size).

ls -r

Recursively list subdirectories encountered

ls -R

Sort by time modified

Most recently modified first before sorting the operands by lexicographical order.

ls -t

A combination of the different options

ls -lart

Enable colorized output.

This option is equivalent to defining CLICOLOR in the environment.

ls -G

See man ls for more options

Remove/delete files and directories

RQ: The rm utility removes symbolic links, not the files referenced by the links.

Delete a file (won't be in Trash)

rm file

Delete the file after confirmation

rm -i file

Delete all the named files

rm file1 file2

Delete directory

Default, ecursive mode

Works also with -r

rm -R myDir
Other mrthod (only if the directory is empty)
rmdir myDir

Delete files/dirs with override

rm -f item

Delete multiple directories

rm -rf {dir1,dir2}

Renamming/moving files

Renames fileName into newFileName

mv fileName newFileName

Renames myDir into myNewDir

mv "myDir" "myNewDir"

Moves fileName in myDir and renames it

mv fileName myDir/newFileName

Shutdown command

Shutdown the system

shutdown -h now

or

halt

Restart the system

shutdown -r now

or

reboot

User management

Add a new user

useradd username

Add password to an user

passwd password

Delete an user

userdel username

Services

You have to be root

(Re)launch a service

Format

service <daemon> start

Example:

service sshd start
service sshd restart

Gets the service status

Format

service <daemon> status

Example:

service sshd status
openssh-daemon (pid  2362) en cours d'exécution…

Stop a service

Format

service <daemon> stop

Example:

service sshd stop

ToolKit © 2017
About