KEMBAR78
PUTTY Usage Commands | PDF | Computer File | File System
0% found this document useful (0 votes)
23 views21 pages

PUTTY Usage Commands

This document provides a comprehensive guide to essential Linux commands for managing files, directories, system updates, and user permissions. It includes commands for viewing system information, managing files and directories, finding files, mounting file systems, and handling disk space. Additionally, it covers user and group management, file permissions, and special attributes, along with examples for each command.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views21 pages

PUTTY Usage Commands

This document provides a comprehensive guide to essential Linux commands for managing files, directories, system updates, and user permissions. It includes commands for viewing system information, managing files and directories, finding files, mounting file systems, and handling disk space. Additionally, it covers user and group management, file permissions, and special attributes, along with examples for each command.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Now we need to know the commands to be able to see directories or files,

create them, delete them, install


applications or update the system packages, among all the functions that
we have available.

We are going to update the server packages. The first thing is to press 'Control + C'.
to complete the process
previously and then write another command, in this case 'yum update', although
this depends on the
Linux distribution installed on the server.

The list of most common folders in Linux systems is:

This is the root.

/usr: here is where the vast majority of existing files in a system are located
Linux, such as documentation, executable, etc.

/bin: here are the commands that all users can use (including the
root).

/sbin: here are the commands that only the root can use.

/dev: here are all the devices of our machine.

/home: place where user accounts are stored. Something similar to 'my
"documents" in Windows.

/lib: here are the libraries needed for the system.

/var: contains variable information, such as system logs


(local mail, etc.)

/tmp: temporary directory.

/etc: here are all the configurations. If you want to modify the
Samba configuration, you just need to edit the text file /etc/samba/smb.conf

root: administrator account.

/boot: this is where everything related to system boot is located.

/media: mount point for locally mounted file systems.

/mnt: it is the predecessor of /media, it is kept only for historical reasons

/proc: virtual file system of process and kernel information.

We can accompany the commands with the attribute "--help" so that they indicate more to us.
detailed options, such as "arch --help".

Most used commands:

System information:

arch → show the architecture of the machine (1).

uname -m → shows the architecture of the machine (2).


uname -r → show the version of the kernel being used.

uname -a → show the complete information.

lsb_release -a → show the complete distribution information.

cat /etc/issue → show the name of the distribution

dmidecode -q → show the components (hardware) of the system.

hdparm -i /dev/hda → show the characteristics of a hard drive.

hdparm -tT /dev/sda → perform a read test on a hard drive.

cat /proc/cpuinfo → show CPU information.

grep -c ^processor /proc/cpuinfo → show number of processors.

cat /proc/interrupts → show the interrupts.

cat /proc/meminfo → check memory usage.

cat /proc/swaps → show swap files.

cat /proc/version → show the kernel version.

cat /proc/net/dev → show network adapters and statistics.

cat /proc/mounts → show the mounted file system.

lscpu → show information about the microprocessor.

lspci -tv → show the PCI devices.

lsusb -tv → show the USB devices.

lshw → list the hardware.

list the hardware.

date → show the system date.

cal 2011 → show the calendar for 2011.

show the calendar for the month of July 2011.

date 041217002011.00 → set (declare, adjust) date and time.

clock -w → save the date changes in the BIOS.

blkid → show information (name, label, UUID, partition type) about the
blocking devices (hard drives, etc.)

Shut down, restart or log out:

shutdown -h now → turn off the system (1).

init 0 → turn off the system (2).


telinit 0 → shut down the system (3).

stop → turn off the system (4).

poweroff → turn off system (5).

shutdown -h hours:minutes & → planned system shutdown.

shutdown -c → cancel a planned system shutdown.

shutdown -r now → restart (1).

reiniciar

log out

skill username → log out (2) (it needs to be executed with permissions)
root

exit → exit the command interpreter (if there is only one, it is equivalent to closing
session).

Manage files and directories:

cd → go to the home directory.

cd /home → change to the directory “/home”.

cd .. → go back one level.

cd ../.. → go back 2 levels.

cd ~user1 → go to the user1 directory.

cd – → go (return) to the previous directory.

pwd → show the path of the current directory.

ls → list the contents of a directory.

ls -F → list the contents of a directory (distinguishing directories with a)


bar)

ls -a lists the files and directories within the current directory.


including hidden files and directories.

ls *[0-9] → list the files and folders that contain numbers.

list recursively the contents of the current directory and all


the subdirectories and files, including the hidden ones, separated by page.

ls -t → Lists the contents of a directory sorting the files by date of


modification.

ls -X → Lists the contents of a directory, sorting files by extension.

ls -l → Lists the contents of a directory. Displays all the information: user,


group, permissions, size, creation date and time.
ls -lh → Lists the directory contents showing the same information as ls -l
but with the size units in KB, MB, etc.

ls -R → Shows the contents of all subdirectories recursively.

ls -S → Lists the contents of a directory sorting the results by size


file.

tree → display files and folders in tree form starting from the root.(1)

lstree → show the files and folders in a tree format starting from the root.
(2)

mkdir dir1 → create a directory named 'dir1'.

mkdir dir1 dir2 → create two directories at once (in the current location).

create a directory structure, if it does not exist.

rm file1 → delete the file 'file1'.

rm -f file1 → delete the file 'file1' in forced mode.

rmdir dir1 → delete the directory ‘dir1’.

rm -rf dir1 → recursively and forcefully remove the directory 'dir1' with
everything it contains.

rm -rf dir1 dir2 → delete two directories with their contents recursively.

rename or move a file or folder (directory).

copy a file to the chosen destination.

cp file1 file2 destination/ → copy two files to the same directory.

copy file1 to file2.

cp dir /* . → copy all the files from one directory into the directory of
current job.

cp -a /tmp/dir1 . → copy a directory into the current working directory.

cp -a dir1 → copy a directory.

cp -a dir1 dir2 → copy two directories at once.

ln -s file1 lnk1 → create a symbolic link to the file or directory.

ln file1 lnk1 → create a physical link to the file or directory.

touch file1 → update the modification date of file1, or create it if it does not exist.

touch -t 0712250000 file1 → modify the real time (creation time) of a


file or directory.

file file1 → output (dump on screen) of the mime type of a text file.

iconv -l → list of known encodings.


iconv -f fromEncoding -t toEncoding inputFile > outputFile → creates a new form
from the input file assuming it is encoded in fromEncoding and
converting it to ToEncoding.

Find files:

find / -name file1 → search for file and directory starting from the root of the system.

find / -user user1 → find files and directories belonging to the user
user1

find /home/user1 -name *.bin → search for files with the '.bin' extension inside
directory / home/user1.

find /usr/bin -type f -atime +100 → find unused binary files in the
last 100 days.

find /usr/bin -type f -mtime -10 → search for files created or modified within
the last 10 days.

find / -name *.rpm -exec chmod 755 '{}'; → search for files with the extension '.rpm' and
modify permissions.

modify permissions recursively


to all files under the current directory (2)

find / -xdev -name *.rpm → Search for files with the '.rpm' extension ignoring the
removable devices such as CD-ROM, pen drive, etc.

find . -maxdepth 1 -name *.jpg -print -exec convert "{}" -resize 80x60
group resized files in the current directory and
send them to directories in thumbnail views (requires conversion from
ImageMagick.

find /tmp/dir1 -depth -regextype posix-extended -regex '.*(s+|:+|+|>+|<+|»+|*+|?


+||+).*’ -execdir rename ‘s/(s+|:+|\+|>+|<+|”+|*+|?+||+)/_/g' «{}» ;→ rename
recursively all directories and files under /tmp/dir1, changing the
spaces and other strange characters replaced by underscores.

locate *.ps → finds files with the '.ps' extension executed first with the
command updatedb.

whereis halt → show the location of a binary, help, or source file. In


this case asks where the halt command is.

which command → show the full path to a command.

Mounting a file system:

mount /dev/hda2 /mnt/hda2 → mount a disk called hda2. First, verify the
existence of the directory '/mnt/hda2'; if it does not exist, you must create it.

umount /dev/hda2 → unmount a disk called hda2. (It is necessary to exit first)
/mnt/hda2.

fuser -km /mnt/hda2 → force unmount when the device is busy.

umount -n /mnt/hda2 → run the unmount without reading the /etc/mtab file. Useful
when the file is read-only, or the hard drive is full.

mount a floppy disk.

mount a cdrom / dvdrom.

mount /dev/hdc /mnt/cdrecorder → to mount a rewritable CD or a DVD-ROM.

mount /dev/hdb /mnt/cdrecorder → mount a rewritable CD / DVD-ROM (a DVD).

mount -t udf,iso9660 -o loop file.iso /mnt/cdrom → mount an image file of


an optical medium (such as a CD or DVD in ISO format).

mount -t vfat /dev/hda5 /mnt/hda5 → mount a FAT32 filesystem.

mount -t ntfs-3g /dev/hda5 /mnt/hda5 → mount an NTFS file system.

mount /dev/sda1 /mnt/usbdisk → mount a usb pen-drive or a memory (without


specify the type of file system.

Disk space:

df -h → show a list of mounted partitions. List the available space


on the disk.

df -hs shows the space that directories occupy.

show the size of files and directories sorted by


size.

Estimate the space used by the directory 'dir1'.

du -sk * | sort -nr → show in descending order the size of the files and
subdirectories in the current location, in KiB.

du -h --max-depth=1 | sort -nr → display in descending order the size of all


subdirectories in the current location, using adaptable units of measurement.

du -k --max-depth=1 | sort -k1 -nr | awk '{printf "%.3f GiB ", $1/(1048576), $2}'
less → display the size of directories and files in descending order,
GiB.

rpm -q -a –qf '%10{SIZE}t%{NAME}n' | sort -k1,1n → show the space used by the
RPM packages installed organized by size (Fedora, Redhat and others).

dpkg-query -W -f='${Package}t${Installed-Size}n' | sort -k 2 -nr | grep -v


uninstall | head -n 25 | awk '{printf "%.3f MBt%sn", $2/(1024), $1}' → show (in
Debian or derived distributions) a list of the 25 installed packages that
more space they consume (in descending order).

Users and groups:

groupadd group_name → create a new group.

groupdel group_name → delete a group.

groupmod -n new_group_name old_group_name → rename a group.

adduser user1 → Create a new user using a wizard.


useradd -c "First Last" -g admin -d /home/user1 -s /bin/bash user1
Create a new user belonging to the 'admin' group.

useradd usuario1 → create a new user.

userdel -r user1 → delete a user, removing their Home directory.

usermod -c "FTP User" -g system -d /ftp/user1 -s /sbin/nologin user1


change the attributes of a user.

Add user user1 to other groups


existing ones, to increase their permissions (in this case, to add the possibility of
connect devices, configure and use the modem

change password

passwd user1 → change the password of 'user1' (only executable as


superuser).

chage -E 2011-12-31 user1 → set an expiration date for the user's password.
this case states that the key expires on December 31, 2011.

check the correct syntax of the file format of '/etc/passwd' and the
existence of users.

grpck → check the correct syntax and format of the file '/etc/group' and the
existence of groups.

newgrp group1 → registers a new group to change the default group of


the files created recently.

File permissions (use "+" to add permissions and "-" to remove):

ls -lh → Show permissions.

ls /tmp | pr -T5 -W$COLUMNS → divide the terminal into 5 columns.

chmod ugo+rwx directory1 → set read, write (w) and permissions


execution(x) to the owner (u), to the group (g), and to others (o) on the directory
directory1

remove read, write (w) and execute (x) permission


to the group (g) and others (o) on the directory 'directory1'.

chown user1 file1 → change the owner of a file.

chown -R user1 directory1 → change the owner of a directory and all its subdirectories
files and directories contained within.

chgrp group1 file1 → change group of files.

change the owner user and group of a file.

find / -perm -u+s → view all system files with SUID set.

chmod u+s /bin/file1 → set the SUID bit on a binary file. The user who
the file execute also has the same permissions as the owner.
disable the SUID bit on a binary file.

chmod g+s /home/public → set an SGID bit on a directory – similar to SUID but
by directory.

chmod g-s /home/public → disable an SGID bit on a directory.

chmod o+t /home/public → sets a STICKY bit on a directory. Allows deletion


of files only to the legitimate owners.

chmod o-t /home/public → disable a STICKY bit in a directory.

Special attributes in files (use '+' to set permissions and '-' to remove)
remove):

chattr +a file1 → allows writing to a file only in append mode.

chattr +c file1 → allows a file to be compressed / decompressed


automatically.

chattr +d file1 → ensures that the program ignores deleting the files during the
backup.

chattr +i file1 → makes the file immutable or unchangeable, so that it does not
it can be deleted, altered, renamed, or linked.

chattr +s file1 → allows a file to be deleted securely.

chattr +S file1 → ensures that a file is modified, the changes are written
in synchronous mode like with sync.

chattr +u file1 → allows you to recover the content of a file even if it is


canceled.

lsattr → show special attributes.

Compressed files and archives:

7za a -mx=9 -ms=on -mhe=on -p compressedarchive directory1 file1 file2


compress a directory and two files in 7zip format, with solid compression
maximum, and password protection (the 7z extension is added automatically).

7za x compressedarchive.7z → extract a compressed file in 7zip (7zip also


allows decompressing other formats, such as zip, for example.

bunzip2 file1.bz2 → decompresses into a file called 'file1.bz2'.

bzip2 file1 → compresses a file named 'file1'.

gunzip file1.gz → decompresses a file named 'file1.gz'.

gzip file1 → compress a file called 'file1'.

gzip -9 file1 → compress with maximum compression.

create a .rar file called 'file1.rar'.

compress 'file1', 'file2' and 'dir1'


simultaneously.
extract rar file.

unrar x file1.rar → decompress rar file.

tar -cvf archive.tar file1 → create an uncompressed tarball.

create an archive containing 'file1',


'file2' and 'dir1'.

tar -tf archive.tar → show the contents of a file.

tar -xvf archive.tar → extract a tarball (if the file is also compressed with
gzip, bzip2 or xz, decompress it automatically.

extract a tarball to /tmp.

create a bzip2 compressed tarball.

extract a .tar file compressed in bzip2

tar -cJvf archive.tar.xz dir1 → create a compressed tarball in xz.

XZ_OPT=-9e tar -cJvf archive.tar.xz dir1 → create a compressed tarball in xz (with


maximum compression.

extract a tar file compressed in xz.

create a gzip compressed tarball.

create a gzip compressed tarball, but


using pigz, which compresses in parallel taking advantage of all the cores of the
microprocessors of the equipment.

GZIP=-9 tar -czvf archive.tar.gz dir1 → create a gzip compressed tarball


maximum compression.

extract a tar file compressed in gzip.

create a compressed file in .zip.

zip -r file1.zip file1 file2 dir1 → compress, in .zip, multiple files and
simultaneous directories.

unzip a .zip file.

rpm packages (Red Hat, Fedora, and similar):

rpm -ivh package.rpm → install an rpm package.

rpm -ivh --nodeps package.rpm → install an rpm package ignoring dependencies


dependencies.

rpm -U package.rpm → update an rpm package without changing its configuration


files.

rpm -F package.rpm → update an rpm package only if it is installed.

rpm -e package_name.rpm → remove an rpm package.


rpm -qa → show all rpm packages installed on the system.

rpm -qa | grep httpd → show all rpm packages with the name 'httpd'.

rpm -qi package_name → get information on a specific installed package.

rpm -qg "System Environment/Daemons" → show the rpm packages of a group


software.

rpm -ql package_name → allows you to list the files that are installed in a
rpm package.

rpm -qc package_name → allows listing the configuration of the files that are
installed in an rpm package.

rpm -q package_name --whatrequires → show the requested dependency list for


an rpm package.

rpm -q package_name --whatprovides → shows the capability provided by an rpm package.

rpm -q package_name --scripts → show the scripts started during the


installation / removal.

rpm -q package_name --changelog → show the revision history of a package


rpm.

rpm -qf /etc/httpd/conf/httpd.conf → check which rpm package belongs to a


given file.

rpm -qp package.rpm -l → shows the list of files provided by an rpm package that still
it has not been installed.

rpm –import /media/cdrom/RPM-GPG-KEY → import the digital signature of the key


public.

rpm –checksig package.rpm → verify the integrity of an rpm package.

rpm -qa gpg-pubkey → verify the integrity of all installed rpm packages.

rpm -V package_name → check the file size, licenses, types,


propietario

rpm -Va → check all installed rpm packages on the system. Use with
caution.

rpm -Vp package.rpm → verify an rpm package that is not installed yet.

rpm2cpio package.rpm | cpio --extract --make-directories *bin → extract file


executable from an rpm package.

rpm -ivh /usr/src/redhat/RPMS/`arch`/package.rpm → install a built package


from an rpm source.

rpmbuild –rebuild package_name.src.rpm → build an rpm package from a source


rpm.

Yum package updater (Fedora, Redhat and others):


yum install package_name → download and install an rpm package.

This will install a YUM and will try to resolve


all the

dependencies, using the repositories.

yum update → update all the installed rpm packages on the system.

yum update package_name → modernize / update an rpm package.

yum remove package_name → remove an rpm package.

yum list → list all installed packages on the system.

yum search package_name → Find a package in an rpm repository.

clean cache rpm by deleting downloaded packages.

To completely purge all header files


that the system uses to resolve the dependency.

To remove all cached files and files at once.


of header.

Deb package management (Debian, Ubuntu, and others):

dpkg -i elpaquete.deb → install / update a package.

dpkg -r elpaquete → remove a deb package from the system.

dpkg -l → show all the deb packages installed in the system.

dpkg -l | grep httpd → show all deb packages with the name 'httpd'.

dpkg -s thepackage → obtain information on a specific installed package in the


system.

dpkg -L thepackage → show the list of files used by an installed package


the system.

dpkg -c elpaquete.deb → show contents of a package (not necessarily


installed).

dpkg -S /bin/ping → check which package a given file belongs to.

Package updater apt and aptitude (Debian, Ubuntu and others):

install / update a deb package.

install / update a deb package from a


cdrom.

update the package list.

apt-get upgrade → update all installed packages.

apt-get remove package_name → remove a deb package from the system.


apt-get check → verify the correct resolution of dependencies.

apt-get clean → clean the cache from downloaded packages.

apt-cache search searched-package → returns a list of packages that correspond to the


series "wanted packages".

aptitude search package → searches for a package by name.

aptitude search ~dpackage → searches for a package by description.

aptitude show package | less → shows information about a package.

aptitude install package1 package2 … → installs multiple packages along with their dependencies
and recommendations.

aptitude -R install package → installs a package with its dependencies, but without the
recommendations.

aptitude why package → lists the reasons why it should be installed


package.

aptitude why-not package → lists the reasons why it cannot be installed


package.

aptitude -rsvW install package → simulates the installation of a package with its
dependencies and recommendations, detailing each one.

aptitude remove package → uninstalls a package.

aptitude purge package → uninstalls a package and cleans it from the cache.

aptitude clean → cleans the package cache.

apt-file -xi search 'sql' → searches for all packages that contain a file or
directory with the SQL expression (without considering uppercase or lowercase). 3)

View the content of a file:

cat file1 → view the contents of a file starting from the first line.

tac file1 → see the contents of a file starting from the last line.

more file1 → view the contents of a file paginated.

less file1 → similar to the 'more' command but allows you to move forward, backward, and search
(compatible with some vi commands).

head -2 file1 → view the first two lines of a file (or 10, if not specified)
specify the number of lines.

tail -2 file1 → see the last two lines of a file (or 10 if not specified)
the number of lines).

Text manipulation:

cat file1 file2 … | command <> file1_in.txt_or_file1_out.txt → general syntax


for text manipulation using PIPE, STDIN and STDOUT.
cat file1 | command (sed, grep, awk, grep, etc…) > result.txt → general syntax
to manipulate a text from a file and write the result to a new file.

cat file1 | command (sed, grep, awk, grep, etc…) >> result.txt → general syntax
to manipulate text from a file and add the result to an existing file.

grep Aug /var/log/messages → search for the word 'Aug' in the file
/var/log/messages

search for words that begin with 'Aug' in the file


/var/log/messages

grep [0-9] /var/log/messages → select all lines from the file


'/var/log/messages' that contain numbers.

grep Aug -R /var/log/ → search for the string "Aug" in the directory ‘/var/log’ and
below.

sed 's/string1/string2/g' example.txt → replaces the string 'string1' in the string


"string2" in the example.txt file.

sed '/^$/d' example.txt → remove all blank lines from example.txt

sed '/ *#/d; /^$/d' example.txt → remove comments and blank lines from
example.txt

convert the text 'ejemplo' from


lowercase to uppercase.

sed -e '1d' example.txt → removes the first line of the file example.txt

sed -n '/string1/p' → only visualize the lines that contain the word
string1

use extended regular expressions to


swap the order of two strings in all instances that
appear.

Establish character and file conversion:

dos2unix filedos.txt fileunix.txt -> convert a text file format from


MSDOS and UNIX.

unix2dos fileunix.txt filedos.txt → convert a text file format from


UNIX to MSDOS.

recode ..HTML < page.txt > page.html → convert a text file to html.

recode -l | more → show all available format conversions.

File system analysis:

badblocks -v /dev/hda1 → Check for bad blocks on the hda1 disk.

fsck /dev/hda1 → repair / check the integrity of the Linux file system on
the disk hda1.

fsck.ext2 /dev/hda1 → repair / check the integrity of the ext file system
2 on the disk hda1.
e2fsck /dev/hda1 → repair / check the integrity of the ext 2 file system
on the disk hda1.

e2fsck -j /dev/hda1 → repair / check the integrity of the ext filesystem


3 on the disk hda1.

fsck.ext3 /dev/hda1 → repair / check the integrity of the ext file system
3 on the disk hda1.

fsck.vfat /dev/hda1 → repair / check the integrity of the FAT filesystem


on the disk hda1.

fsck.msdos /dev/hda1 → repair / check the integrity of a system file


two on the disk hda1.

dosfsck /dev/hda1 → repair / check the integrity of a dos file system


on the disk hda1.

Format a file system:

mkfs /dev/hda1 → create a Linux filesystem on partition hda1.

mke2fs /dev/hda1 → create a Linux ext 2 filesystem on hda1.

mke2fs -j /dev/hda1 → create a Linux ext3 filesystem (journaled) on the


hda1 partition.

mkfs -t vfat 32 -F /dev/hda1 → create a FAT32 file system on hda1.

fdformat -n /dev/fd0 → format a floppy disk.

mkswap /dev/hda3 → create a swap system file.

Swap system partition:

mkswap /dev/hda3 → create swap system file.

swapon /dev/hda3 → activating a new swap partition.

swapon /dev/hda2 /dev/hdb3 → activate two swap partitions.

Backup:

make a complete backup of the directory


/home

make an incremental backup of the directory


/home

restoring a backup interactively.

scp is used to transfer all


the subdirectories (providing an estimated time and connection speed)
from the local host to the remote host securely. The command v will provide
debugging information that can help diagnose a problem.

rsync -rogpav --delete /home /tmp → directory synchronization.


rsync over SSH tunnel.

rsync -az -e ssh --delete ip_addr:/home/public /home/local → sync a


local directory with a remote directory via SSH and compression.

sync a
remote directory with a local directory via SSH and compression.

dd bs=1M if=/dev/hda | gzip | ssh user@ip_addr 'dd of=hda.gz' → make a backup of


a hard drive on a remote host via SSH.

dd if=/dev/sda of=/tmp/file1 → save the contents of a hard drive to a file.


(In this case the hard drive is 'sda' and the file 'file1').

tar -Puf backup.tar /home/user → perform an incremental backup of the directory


/home/user

tar -czv --exclude=/root/dir1/* -f /var/salvas/cfg_$(date +%F_%H%M).tgz /etc /root


back up the directories /etc and /root (excluding the content of
subdirectory /root/dir1/) in a compressed file, whose name contains the date
and current time.

( cd /tmp/local/ && tar c . ) | ssh -C user@ip_addr 'cd /home/share/ && tar x -p'
copy the contents of a directory to a remote directory via SSH.

( tar c /home ) | ssh -C user@ip_addr 'cd /home/backup-home && tar x -p' → copy
a local directory on a remote directory via SSH.

tar cf – . | (cd /tmp/backup ; tar xf – ) → local copy preserving the licenses and
links from one directory to another.

find /home/user1 -name '*.txt' | xargs cp -av --target-directory=/home/backup/ --


parents → find and copy all files with the extension '.txt' from a
directory to another.

find ~/ -type f -not -iname '*.mp*' -not ( -iregex '.*.mozilla/.*' -o -iregex


‘.*.thumbnails/.*’ ) -prune -mtime 14 -print0 | xargs -0 tar -czf
/var/salvas/myhome.tgz → make a backup of all the files in our
user profiles modified within the last 14 days, excluding the
mp3 files, mpg and similar files, and the directories .mozilla/ and .thumbnails/.

find /var/log -name '*.log' | tar cv --files-from=- | bzip2 > log.tar.bz2


find all files with extension '.log' and create a bzip file.

dd if=/dev/hda of=/dev/fd0 bs=512 count=1 → make a copy of the MBR (Master Boot Record)
Record to a floppy disk.

dd if=/dev/fd0 of=/dev/hda bs=512 count=1 → restore the copy of the MBR (Master Boot
Record saved on a floppy.

CDROM:

cdrecord -v gracetime=2 dev=/dev/cdrom -eject blank=fast -force → clean or erase


a rewritable CD.

mkisofs /dev/cdrom > cd.iso → create an iso image of cdrom on disk.

mkisofs /dev/cdrom | gzip > cd_iso.gz → create a compressed iso image of cdrom in
disco.
mkisofs -J -allow-leading-dots -R -V "Label CD" -iso-level 4 -o ./cd.iso data_cd
create an iso image of a directory.

cdrecord -v dev=/dev/cdrom cd.iso → burn an iso image.

gzip -dc cd_iso.gz | cdrecord dev=/dev/cdrom – → burn a compressed iso image.

mount -t udf,iso9660 -o loop cd.iso /mnt/iso → mount an iso image.

cd-paranoia -B → to carry songs from a cd to wav files.

cd-paranoia - "-3" → carry the first 3 songs of a cd to wav files.

cdrecord –scanbus → scan the bus to identify the scsi channel.

dd if=/dev/hdc | md5sum → make md5sum work on a device, like a CD.

eject -v → eject a removable medium or disk, providing additional information.

Working with the network (LAN and WIFI):

ifconfig eth0 → show the configuration of an Ethernet network interface. Allows


also perform a debug of the network system and interfaces.

ifup eth0 → activate the eth0 interface.

ifdown & ifup eth0 → disable the eth0 interface.

ifconfig eth0 192.168.1.1 netmask 255.255.255.0 → configure an IP address.

ifconfig eth0 promisc → configure eth0 in promiscuous mode to receive packets


(sniffing).

dhclient eth0 → activate the 'eth0' interface in DHCP mode.

dhclient -r

Using the -r parameter allows you to release the current IP and obtain a new one from the
DHCP server of your system.

route -n → show routing table.

route add -net 0/0 gw IP_Gateway → configure default entry.

show default gateway


default.

route add -net 192.168.0.0 netmask 255.255.0.0 gw 192.168.1.1 → configure route


static to search the network 192.168.0.0/16.

route del 0/0 gw IP_gateway → remove the static route.

echo 1 > /proc/sys/net/ipv4/ip_forward → activate packet forwarding


IP.

hostname → show the name of the system's host.

host www.example.com → look up the host name to resolve the name to an


e2fsck /dev/hda1 → repair / check the integrity of the ext 2 file system
on the disk hda1.

e2fsck -j /dev/hda1 → repair / check the integrity of the ext filesystem


3 on the disk hda1.

fsck.ext3 /dev/hda1 → repair / check the integrity of the ext file system
3 on the disk hda1.

fsck.vfat /dev/hda1 → repair / check the integrity of the FAT filesystem


on the disk hda1.

fsck.msdos /dev/hda1 → repair / check the integrity of a system file


two on the disk hda1.

dosfsck /dev/hda1 → repair / check the integrity of a dos file system


on the disk hda1.

Format a file system:

mkfs /dev/hda1 → create a Linux filesystem on partition hda1.

mke2fs /dev/hda1 → create a Linux ext 2 filesystem on hda1.

mke2fs -j /dev/hda1 → create a Linux ext3 filesystem (journaled) on the


hda1 partition.

mkfs -t vfat 32 -F /dev/hda1 → create a FAT32 file system on hda1.

fdformat -n /dev/fd0 → format a floppy disk.

mkswap /dev/hda3 → create a swap system file.

Swap system partition:

mkswap /dev/hda3 → create swap system file.

swapon /dev/hda3 → activating a new swap partition.

swapon /dev/hda2 /dev/hdb3 → activate two swap partitions.

Backup:

make a complete backup of the directory


/home

make an incremental backup of the directory


/home

restoring a backup interactively.

scp is used to transfer all


the subdirectories (providing an estimated time and connection speed)
from the local host to the remote host securely. The command v will provide
debugging information that can help diagnose a problem.

rsync -rogpav --delete /home /tmp → directory synchronization.


sudo apt-get install ifplugd
And it works like this:

ifplugstatus
ifplugstatus eth0
Microsoft Windows Networks (Samba):

nbtscan ip_addr → BIOS network name resolution.

nmblookup -A ip_addr → BIOS network name resolution.

smbclient -L ip_addr/hostname → show remote actions of a host in Windows.

Firewall (iptables):

iptables -t filter -L → show all chains of the filter table.

iptables -t nat -L → show all chains of the nat table.

iptables -t filter -F → clear all rules from the filter table.

iptables -t nat -F → clear all rules from the nat table.

iptables -t filter -X → delete any chain created by the user.

iptables -t filter -A INPUT -p tcp --dport telnet -j ACCEPT → allow the


telnet connections to enter.

iptables -t filter -A OUTPUT -p tcp --dport http -j DROP → block the connections
HTTP to log out.

iptables -t filter -A FORWARD -p tcp --dport pop3 -j ACCEPT → allow the


POP connections to a front chain.

iptables -t filter -A INPUT -p tcp -m multiport --dports 80,443,8080 -m state --state


NEW -m limit –limit 4/sec –limit-burst 8 -j ACCEPT → set a limit of 4
requests per second of new connections, with possible occasional bursts (useful
for default denial policies).

iptables -t filter -A INPUT -p tcp -m multiport --dports 80,443,8080 -m state --state


ESTABLISHED,RELATED -m connlimit ! --conlimit-above 6 -j ACCEPT → establish a
limit of 6 simultaneous connections per device to our web server (useful for
default denial policies.

iptables -t filter -A INPUT -j LOG --log-prefix "DROP INPUT" → logging a


input string.

iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE → configure a PAT (Port Address Translation)
translation of address) on eth0, hiding forced outgoing packets.
(Indicated for ppp type links).

iptables -t nat -A POSTROUTING -s 192.168.0.127 -o eth0 -j SNAT --to-source


route packets from 192.168.0.127 to other networks through
eth0 and assign them an external IP address. (Indicated for ADSL type links)

iptables -t nat -A PREROUTING -d 192.168.0.1 -p tcp -m tcp --dport 22 -j DNAT --to-


destination 10.0.0.2:22 → redirect the packets addressed from one host to another.
iptables -t nat -S → List all active rules in the nat table.

iptables-save -c > file → Save the rules to a file (including the


packet and byte counters.

iptables-restore -c < file → Restore the rules from a file (including


the packet and byte counters).

Monitoring and debugging:

top → display real-time Linux tasks.

top -i → displays the active processes on the server while hiding the inactive ones.

htop → show and manage tasks with a user-friendly interface.

ps -eafw → shows the Linux tasks.

ps -e -o pid,args --forest → shows Linux tasks in a hierarchical mode.

ps -o pid,cmd -ww -C wget → list all instances of the wget command with their
arguments.

pstree → show a system process tree.

pidof pppd → show the pid of the pppd process.

kill -9 Process_ID → force quit a process and terminate it.

kill -1 Process_ID → force a process to reload the configuration.

killall Process_Name → terminate a process by the command name and not by


ID.

kill -HUP $(ps -A -o state,pid --no-header | grep -e '^[Zz]' | awk '{print $2}' |
xargs) → terminate all zombie processes.

lsof -p $$ → show a list of files opened by processes.

lsof /home/user1 → shows a list of open files in a given path of


system.

strace -c ls >/dev/null → show the system calls made and received


for a process.

strace -f -e open ls >/dev/null → show the library calls.

watch -n1 'cat /proc/interrupts' → display interruptions in real time.

show reboot history.

lsmod → show the loaded kernel modules.

free -m → shows the state of RAM in megabytes.

smartctl -A /dev/hda → monitor the reliability of a hard drive through SMART.

smartctl -i /dev/hda → check if SMART is enabled on a hard drive.


tail -f /var/log/dmesg → shows events inherent to the kernel loading process.

tail -f /var/log/messages → show system events.

multitail –follow-all /var/log/{dmesg,messages} → show two event logs


on the same screen.

Security and Encryption:

base64 /home/file > /home/encoded-file → encodes "file" in 'base64' and


he/she saves it in /home

base64 -d /home/decoded-file > /home/file → decodes 'file-'


"encoded" and saves it in /home

openssl req -x509 -nodes -days 3650 -newkey rsa:1024 -out /etc/millave.crt -
keyout /etc/millave.key → creates a self-signed certificate to encrypt traffic
web with SSL.

htpasswd -c -m /etc/apache2/.htpasswd username → generates a '.htpasswd' file


to protect a website with authentication.

Other useful commands:

apropos keyword → show a list of commands that belong to the words


keys to a program; they are useful when you know what your program does, but
you do not know the name of the command.

man ping → show the online manual pages; for example, in a ping command,
use the '-k' option to find any related command.

man -t ping | ps2pdf – ping.pdf → convert the pages of the ping command manual
in a PDF file (for which it is necessary to have installed Ghostscript).

mkbootdisk –device /dev/fd0 `uname -r` → create a bootable floppy.

gpg -c file1 → encrypt a file with GNU Privacy Guard.

gpg file1.gpg → decrypt a file with GNU Privacy Guard.

wget -r www.example.com → download a complete website.

download a file with the possibility to stop


download and resume later.

curl & get → These two commands allow you to download a file without leaving the
terminal

curl -O website.com/file

wget website.com/file
Start a download at www.example.com/files.iso
any time. In this case, it would start at 9 o'clock.

ldd /usr/bin/ssh → show the shared libraries required by the program


SSH.

alias hh='history' → set an alias for a command. In this case, to invoke


the history with hh.
chsh → change the Shell command.

chsh –list-shells → is a suitable command to know if you have to do remote in


another terminal.

who -a → to show who is registered, and print the last system time
importation
produced by init, current operation and latest changes of the system clock.

echo "128*1024*1024" | bc → calculate the size in bytes of 128 from the console
MiB.

sudo !! → execute the last typed command as superuser.

clear → clean the screen.

command > outputfile.txt 2>&1 → executes a command and redirects the output to a
file, combining both STDOUT and STDERR in this.

uncomando > outputfile.txt 2> errorfile.txt → runs a command,


redirect the output (STDOUT) to a file, and the errors (STDERR) to another.

uncomando | tee archivodesalida.txt -> executes a command, displays the output in the
screen and simultaneously writes it to a file.

You might also like