Linux Command
Linux Command
GNU/Linux most wanted Displaying file contents Archiving Access the full manual page of a command:
man grep
Concatenate and display file contents: Comparing 2 directories: Create a compressed archive (tape archive):
Summary of most useful commands cat file1 file2 diff r dir1 dir2 tar zcvf archive.tar.gz dir Misc commands
tar jcvf archive.tar.bz2 dir
©Copyright 2014-2005, Free Electrons. Display the contents of several files (stopping Looking for files tar Jcvf archive.tar.xz dir Basic command-line calculator
Free to share under the terms of the Creative Commons at each page): tar lzma cvf archive.tar.lzma bc l
Attribution-ShareAlike 3.0 license more file1 file2 Find all files in the current (.) directory and its
(http://creativecommons.org) less file1 file2 (better: extra features) subdirectories with log in their name: Test (list) a compressed archive: Basic system administration
find . name “*log*” tar tvf archive.tar.[gz|bz2|lzma|xz]
Sources, translations, updates, command and concepts Display the first 10 lines of a file: Change the owner and group of a directory and
details on our free training materials: Find all the .pdf files in dir and subdirectories
head 10 file Extract the contents of a compressed archive: all its contents:
http://free-electrons.com/training/intro_unix_linux
and run a command on each: tar xvf archive.tar.[gz|bz2|lzma|xz] sudo chown R newuser.newgroup dir
Display the last 10 lines of a file: find . name “*.pdf” exec xpdf {} ';'
Thanks to Michel Blanc, Hermann J. Beckers and Thierry
Grellier.
tail 10 file tar options: Reboot the machine in 5 minutes:
Quick system-wide file search by pattern c: create sudo shutdown r +5
Latest update: May 26, 2014 File name pattern matching (caution: index based, misses new files): t: test
locate “*pub*” Shutdown the machine now:
Concatenate all “regular” files: x: extract
sudo shutdown h now
Handling files and directories cat * j: on the fly bzip2 (un)compression
Redirecting command output J: on the fly xz (un)compression Display all available network interfaces:
Create a directory:
mkdir dir Concatenate all “hidden” files: Redirect command output to a file: z: on the fly gzip (un)compression ifconfig a
cat .* ls *.png > image_files
Create nested directories: Handling zip archives Assign an IP address to a network interface:
mkdir p dir1/dir2 Concatenate all files ending with .log: Append command output to an existing file: zip r archive.zip <files> (create) sudo ifconfig eth0 207.46.130.108
cat *.log ls *.jpg >> image_files unzip t archive.zip (test / list)
Changing directories: Bring down a network interface:
List “regular” files with bug in their name: unzip archive.zip (extract) sudo ifconfig eth0 down
cd newdir Redirect command output to the input of
ls *bug* another command:
cd .. (parent directory) Printing Define a default gateway for packets to
cd (previous directory) cat *.log | grep error
List all “regular” files ending with . and a machines outside the local network:
cd (home directory) single character: Send PostScript or text files to queue: sudo route add default gw 192.168.0.1
cd ~bill (home directory of user bill) ls *.? Job control lpr Pqueue f1.ps f2.txt (local printer)
Delete the default route:
Show all running processes: List all the print jobs in queue:
Print the working (current) directory: sudo route del default
pwd
Handling file contents ps ef lpq Pqueue
Show only the lines in a file containing a given Test networking with another machine:
Live hit-parade of processes (press P, M, T: sort Cancel a print job number in queue:
Copy a file to another: substring: ping 207.46.130.108
cp source_file dest_file by Processor, Memory or Time usage): cancel 123 queue
grep substring file top Create or remove partitions on the first IDE
Copy files to a directory: Print a PDF file: hard disk:
Case insensitive search: Send a termination signal to a process: pdf2ps doc.pdf
cp file1 file2 dir grep i substring file fdisk /dev/hda1
kill <pid> (number found in ps output) lpr doc.ps
Copy directories recursively: Showing all the lines but the ones containing a Create (format) an ext3 filesystem:
cp r source_dir dest_dir Have the kernel kill a process: View a PostScript file: mkfs.ext3 /dev/hda1
substring: kill 9 <pid> ps2pdf doc.ps
rsync a source_dir/ dest_dir/ grep v substring file xpdf doc.pdf Create (format) a FAT32 filesystem:
Create a symbolic link: Kill all processes (at least all user ones): mkfs.vfat v F 32 /dev/hda2
Search through all the files in a directory: kill 9 1
ln s linked_file link grep r substring dir User management Mount a formatted partition:
Rename a file, link or directory: Kill a graphical application: mkdir /mnt/usbdisk (just do it once)
Sort lines in a given file: List users logged on the system:
mv source_file dest_file xkill (click on the program window to kill) sudo mount /dev/uba1 /mnt/usbdisk
sort file who
Remove files or links: Sort lines, only display duplicate ones once: File and partition sizes Show which user I am logged as: Mount a filesystem image (loop device):
rm file1 file2 whoami sudo mount o loop fs.img /mnt/fs
sort u file (unique) Show the total size on disk of files or
Remove empty directories: directories (disk usage): Show which groups user belongs to: Unmount a filesystem:
rmdir dir Changing file access rights du sh dir1 dir2 file1 file2 groups user sudo umount /mnt/usbdisk
Remove non-empty directories: Add write permissions to the current user: Number of bytes, words and lines in file: Tell more information about user: Check the system kernel version:
rm rf dir chmod u+w file wc file (word count) uname a
finger user
Add read permissions to users in the file group: Show the size, total space and free space of the
Listing files chmod g+r file
Switch to user hulk:
current partition: su hulk
List all “regular” files (not starting with .) in df h .
Add execute permissions to other users:
the current directory: Switch to super user (root):
chmod o+x file Display these info for all partitions:
ls su (switch user)
Add read + write permissions to all users: df h su (keep same directory and environment)
Display a long listing: chmod a+rw file
ls l Compressing Time management
Make executable files executable by all:
List all the files in the current directory, chmod a+rX * Compress a file: Wait for 60 seconds:
including “hidden” ones (starting with .): gzip file (.gz format) sleep 60
ls a Make the whole directory and its contents bzip2 file (.bz2 format, better)
accessible by all users: lzma file (.lzma format, best compression) Show the current date:
List by time (most recent files first): chmod R a+rX dir (recursive) date
ls t xz file (.xz format, best for code)
Count the time taken by a command:
List by size (biggest files first) Comparing files and directories Uncompress a file:
time find_charming_prince cute rich
ls S gunzip file.gz
Comparing 2 files: bunzip2 file.bz2
List with a reverse sort order: diff file1 file2 unlzma file.lzma Command help
ls r unxz file.xz Basic help (works for most commands):
Comparing 2 files (graphical):
Long list with most recent files last: gvimdiff file1 file2 grep help
ls ltr tkdiff file1 file2