Linux Terminal Memo

March 7, 2017

Here's a list of useful Linux commands (Debian Distribution).

Disk Cleaning

Show top 10 biggest subdirs in the current dir:
du -sk * | sort -nr | head -10

Check if you have old kernels for deletion
ls -lh /boot

Packages Cleaning
sudo apt-get autoremove
sudo apt-get autoclean

See list of all installed packages, sorted by size
dpkg-query -W --showformat='${Installed-Size} ${Package}\n' | sort -nr | less

Clean unused language files with translations
sudo apt-get install localepurge

Check content of /var/tmp/
du -sh /var/tmp/

Check also
man deborphan

Searching for files

find / -type f -size +1024k
find / -size +50000 -exec ls -lahg {} \;

Check for deleted files
lsof | grep -i deleted

Viewing files

Display the contents of the current directory
ls

Display also hidden files and hidden directories
ls -a

Copying files

Copy filename into directory /path/dir_name
cp filename /path/dir_name
cp -r dir_name /path/dir_name2

Copy the entire dir_name into /path/dir_name2
cp filename1 filename2 /path/dir_name

Deleting files

Remove a file or directory called name
rm name

Remove an entire directory as well as its included files and subdirectories
rm -r name

Moving files

Move filename into /path/dir_name
mv filename /path/dir_name

Rename filename1 to filename2
mv filename1 filename2

Displaying files

Display filenames contents
cat filename

Display filename in pages. Use spacebar to view next page
more filename

Display filenames first 10 lines
head filename

Display filenames first 15 lines
head -15 filename

Display filenames last 10 lines
tail filename

Display filenames last 15 lines
tail -15 filename

Path Navigation

Display current directory
pwd

Change to directory /path/dir_name
cd /path/dir_name

Go 1 directory up
cd ..

Directories

Create directory dir_name
mkdir dir_name

Delete directory dir_name
rmdir dir_name

Finding files and text within files

Update (create first time used) a database of all files under the root directory
updatedb

Find file filename searching in the database
locate filename

Starting from the root directory search for the file called filename
find / -name filename

Same as above but search for file containing the string filename
find / -name *filename

Starting from /path/dir_name search for all files containing string
grep string /path/dir_name

Search $path for application app_name
which application_name

Search $path, man pages and source files for application_name
whereis application_name

Archives

Decompress tzg file
tar -xzf filename.tgz

Decompress tar.gz file
tar -xzf filename.tar.gz

Decompress tar.bz2 file
tar -xjf filename.tar.bz2

Compress directory /path/dir_name to filename.tar
tar -czf filename.tar /path/dir_name

Compress /path/dir_name to filename.tar.gz
gzip -c filename > filename.gz

Compress /path/dir_name to filename.tar.bz2
bzip2 -c filename > filename.bz2

Using rpm files

Install rpm called package.rpm
rpm -hiv package.rpm

Force Install of rpm called package.rpm
rpm -hiv –force package.rpm

Upgrade rpm called package.rpm
rpm -hUv package.rpm

Delete rpm called package.rpm
rpm -e package.rpm

List files in not-installed rpm called package.rpm
rpm -qpil package.rpm

List files in installed rpm called package.rpm
rpm -ql package.rpm

List installed rpms containing the string str
rpm -q str

Display the rpm that contains application application_name
rpm -qf /path/application_name

Starting and Stoping

Start the X system
startx

Shutdown the system now and do not reboot
shutdown -h now
halt Same as above

Reboot
shutdown -r now
reboot

Reboot in 10 minutes
shutdown -r +10

Mounting filesystems

Mount the first partition 1 of the first hard disk drive a which is in fat32 vfat dormat under /mnt/c_drive directory
mount -t vfat /dev/sd(a)(1) /mnt/c_drive

Mount cdrom under /mnt/cdrom directory
mount -t iso9660 /dev/cdrom /mnt/cdrom

Unmout the above
umount /mnt/hda1

User administration

Display users currently logged in
users

Create a new user called username
adduser username

Define password for user called password
passwd password

List logged-in users
who

Display current user
whoami

Displays info about user username
finger username

Log in as root from current login
su

Log in as root from current login and take root’s path
su -

Exit from console login (ie, logout)
exit

Processes management

Execute command in the foreground command

Execute command in the background
command &

Suspend a program
ctrl+z

Interrupt a program
ctrl+c
List all processes
ps

Kill process with id pid
kill -9 pid

Monitor processes in real time
top

Networking

List the system’s hostname
hostname

Set/Display network information
ifconfig

Resolves ip’s hostname
host ip

Check if ip/hostname is reachable
ping ip/hostname

Find network path to ip/hostname
traceroute ip/hostname

System Information

General system information
uname -a

List partition tables
fdisk -l

Copy filename into directory /path/dir_name
cp filename /path/dir_name

List filesystem disk space usage
df -T -h

List PCI devices
lspci

List USB devices
lsusb

Display RAM+Swap usage
free -m