Posts Tagged ‘commands’

df: displays file system disk space usage for all partitions

free: displays the amount of free and used memory in the system

top: displays running processes

uname -a: prints system information

lsb_release -a: prints version information information of the Linux release currently running

ifconfig: reports system network interfaces

iwconfig: reports wireless network adapters

ps: view all running processes

lspci: lists all pci devices

lsusb: lists usb devices

lshw: lists hardware

Important Files

/usr/share/zoneinfo — contains time zone information for many different regions
/etc/timezone — holds the timezone
/etc/localtime — a symbolic link to the correct file in /path/usr/share/zoneinfo/
etc/ntp.conf — configuration file for NTP
/etc/ntp.drift — where NTP stores correction for local clock being fast/slow

Commands

date — display/set system time
hwclock — query and set the hardware clock (RTC)
ntpdate — used to set system date and time via NTP

The Hardware and The System Clock

The hardware or Real Time Clock (RTC) hardware clock is located on the motherboard.
System Clock is maintained in the Linux kernel and is used while the system is running.

hwclock options

Set the system time from the RTC

hwclock -s or hwclock --hctosys

Set the RTC from the system time

hwclock -w or hwclock --systohc

Display the contents of the RTC

hwclock -r or hwclock --show 

Ajust the RTC for clock drift

hwclock -a or hwclock --adjust

The file /etc/adjtime is used to hold information about the extent to which (and direction) the RTC drifts.

find – search a file from a certain criteria.
syntax:

find directory criteria [-exec command {} \;]

e.g.:

remove all files belonging to user 502:
find / - type f -user 502 –exec rm –f {} \;

locate – list all files and directories that match the expression.
syntax:

locate string

locate queries the /var/lib/slocate/slocate.db database. This database is kept up to date via a daily cron job which runs updatedb. updatedb read /etc/updatedb.conf file. The Security Enhanced version of the GNU locate is slocate.

whereis – return the full path to source or binaries as well as documentation files matching string by scanning the PATH variable as well as a number of well known locations.
syntax:

whereis string

which – return the full path to the file called string by scanning the directories defined in the user’s PATH variable only. which is only used to find commands.
syntax:

which string

apropos – search manual page names and description.
syntax:

apropros string

whatis – display manual page description.
syntax:

whatis string

Text filters are used to modify the output of a file. They are helpful when searching for a specific keyword  and viewing a different output format from a file.

cat command is used to display the contents of a file. There are helpful options in viewing the file contents. The most commonly used are:
–   n: number each line of output
–   b: number only non-blank output line
–   A: show carriage return

Another function of cat is it can be used as a rudimentary text editor. How? By using a redirect. A redirect is used with a greater than sign >. Here’s a sample:

cat > sample-file
I'm typing the content of the sample-file.

Use CTRL+D to to save the content and end the interactive input.

You can also use the tac command to read text from the last to the first line. Obviously it’s the opposite of cat.

When analyzing log files, head or tail commands are helpful. By default, both output 10 lines of  text.

head file: outputs the first 10 lines
head [-20] file: outputs the first 20 lines
tail [-20] file: outputs the last 20 lines
head [+25] file: list text starting at line 25
tail -f file: continuously read a file, for real-time monitoring.

The wc utility for word count. It counts the number of bytes, words, and lines in files.
Options for wc output are:
–   l: count number of lines
–   w: count number of words
–   c or m: count number of bytes or characters

Another utility is nl. It functions like the cat -n.

nl -ba file: number lines including blanks
nl -bt file: number lines with text

The expand utility is used to replace TABs with spaces while the unexpand command is used for reverse operation.

To view binary files, use the hexdump utility. od utility may also be used.

To split a file into a smaller file, use the split utility.

split -1 5 file: create files xaa to xae w/ 5 lines each
split -1 5 file name-: create files xname-aa to name-ae

To not display the consecutive identical lines, use the uniq utility.

To extract a range of characters or fields from each line of text, use the cut utility.

cut - c range1, range 2 file: to manipulate characters
cut -d delimeter -f fields --output-delimeter=" " file

paste utility concatenates two files next to each other.

paste file1 file2

join utility outputs match fields between two files.

join -1 field_num -2 field_num file1 file2

sort utility arrange text in alphabetical order. -n option is used for numerical sort.

fmt utility to format output lines. fmt options are:
–   w: number of characters per line
–   s: split long lines but do not refill
–   u: place one space between each word and two spaces at the end of the sentence.

pr utility to paginate a file.

tr utility to translate one set of character ot another.

tr 'character 'character' < file

sed utility to search and replace patterns in text

sed '/ search_pattern/ new_pattern' file: substitute a pattern
sed 's/search_pattern/ new_pattern/g' file: substitute a pattern
 s - substitute
 g - globally; force substitution
sed '/key/new_pattern/g' file: if string exists, substitute a pattern

options for sed
–   e: execute following command
–   f: read commands from file
–   n: do not print out unedited lines
commands of sed
d – delete an entire line
r – read a file and append to output
s – substitute
w – write output to file

Commands explained below aren’t necessarily the commands first encountered when running the Linux OS. It just happen that these are the first set of commands I met. All of the commands are helpful once you started reading and exploring Linux.

which command: display the file location of the indicated commands

whereis file: locates source/binary and manuals sections for specified files.

file directory: display the type of file (txt, dll, exe, bin)

touch filename: create a file

free: display memory information (-m)

lsusb: check connected usb devices (-v, -vv)

lspci: check the controllers (lspci | less, lspci | grep search)

lsmod: list all modules of drivers

modinfo module: display module information

ps -uxa: see running processes

ldd file: print shared library dependencies

cat filename: look, modify, or combine a file

less filename: display output one screen at a time

grep string filename: display output that contain the entered string. Use braces to ignore case-sensitve property