Misc

xargs

xargs [-0] [-l<n>] <command>

-0 : Use null character as argument separator (default: newline);
for supporting filenames with blank spaces or newlines;
used in combination with -print0 for find command
-l : (lower-case "L") Pass at most n arguments at a time to the command
  • Runs a specified command on files whose names appear in the program's input stream
find -type f -name "*.txt" | xargs ls
find -type f -name "*.txt" -print0 | xargs -0 ls
# For each *.txt file containing the text "hello"
find -type f -name "*.txt" | xargs grep -l 'hello' | xargs -l1 ls

-exec, xargs, $(...)

# The following three lines are roughly equivalent:
find -type d -exec ls -adl {} \;
find -type d | xargs ls -adl
ls -adl $(find -type d)

# Note: only the first line can handle directories and filenames containing blankspaces
# The following two lines are equivalent:
find -type d | xargs echo
echo $(find -type d)
  • -exec calls the second command once for each file found.
  • xargs and $(...) (command substitution), call the second command only once, passing the names of all of the files at once.
  • `...` is another syntax for command substitution, identical to $(...).

cron

  • Time-based job scheduler
  • Daemon: crond
  • Configuration file: /etc/crontab
min hr dom mon dow   command

minute:      0-59
hour:        0-23
day of month:  1-31
month:       1-12, jan, feb, ...
day of week:   0-6,  sun, mon, ...
Operators
, :  List of values (1,3,4,7,8)
- :  Range of values (1-6)
* :  Any value
/ :  Every nth (*/3)
Shortcuts
@yearly
@annually     # Same as @yearly

@monthly
@weekly

@daily
@midnight     # Same as @daily

@hourly
@reboot     # Run once, at startup

Sorting files

  • sort <input-file> [-urnbd] --output=<output-file>
Options:
  • -u : Remove duplicates
  • -r : Sort in reverse order
  • -n : Numeric sort
  • -b : Ingnore leading blanks (doesn't remove them though)
  • -d : Dictionary order (sorts only by letters and blankspaces)

Comparing files

  • diff <file> <file>
  • cmp <file> <file>

Symbolic links

  • ln -s <existing-file> <new-link>
Examples
  • ln -s /usr/bin/perl /usr/local/bin/perl

App documentation

  • <app> --help  : Default help; list of syntax and command-line arguments
  • man <app>  : Help manual
  • man -k <app>  : List of related help manuals

Processes

  • Control-C  : Terminates a running process
  • Control-Z  : Suspends a running process and puts it in the background; to resume the process in the background, type "bg"
  • bg: Puts a process in the background
  • fg: Puts a process in the foreground
  • ps  : Shows all processes running on the current terminal (tty)
  • ps x  : Shows all processes running on a different terminal or no terminal
  • ps -e  : Shows all processes
  • ps aux  : Shows all processes started by any user, and which user started them
  • pstree  : Shows process dependencies
  • kill [-<signal>] <pid>  : Send a signal to the process whose process-id is <pid>; it's usually good to start with the highest pid when there are a number of processes that need to be terminated
signal
  • 1, sighup  : Send the process a hangup signal, which the program can optionally act upon
  • 15, sigterm  : Send the process a termination signal, which the program can optionally act upon (default) (same as typing control-C)
  • 9, sigkill  : Terminate the process immediately

Background processes

  • <command> &

Command prompt

PS1=”[\[suHhWw]]*[$>]”

s shell
u user
H hostname
h hostname up to the first “.”
w full dir
W cur dir
  • Set the PS1 environment variable
PS1=”[\W]$ ”   # [cur-dir]$ _

End-of-line characters


  • dos2unix <file>  : Converts end-of-line characters from DOS to Linux
  • unix2dos <file>  : Converts end-of-line characters from Linux to DOS
# Identify text files in DOS or Mac format
find -type f -exec grep -Pl $'\r\n' {} \;
# Convert text files from DOS format to Unix format
dos2unix inputfile
tr -d '\r' < inputfile > outputfile
# Convert text files from Mac format to Unix format
tr '\r' '\n' < inputfile > outputfile
# Convert text files from Unix format to DOS format
unix2dos inputfile
tr '\n' '\r\n' < inputfile > outputfile
# Convert text files from Unix format to Mac format
tr '\n' '\r' < inputfile > outputfile

Users

Add
  • /usr/sbin/groupadd [-g <gid>] <group>  : Add a group
  • /usr/sbin/useradd [-g <group>] [-u <uid>] [-p password] <user>  : Add a user
Update
  • /usr/sbin/groupmod [-g <gid>] [-n <new name>] <group>
  • /usr/sbin/usermod [-g <group>] [-u <uid>] [-p password] <user>
Delete
  • /usr/sbin/groupdel <group>  : Delete a group
  • /usr/sbin/userdel <user>  : Delete a user
Password
  • passwd  : Change the password for the current user
  • passwd <user>  : Change the password for the specified user
Identify
  • whoami  : List the current user
  • groups  : List the groups the current user belongs to
  • w  : Displays the currents users of the system and what each one is doing

Misc

  • ~/  : Precede a path or filename with "~/" to specify a location relative to the current user's home directory
  • ./  : Precede a filename with "./" to execute a file located in the current directory (usually not needed if the current directory happens to be listed in the system path).
  • /sbin/lsmod  : Display all modules currently loaded in memory
  • /sbin/insmod <module>  : Loads a module into memory
  • /sbin/rmmod <module>  : Removes a module from memory
  • sleep <n>[s,m,h,d]  : Pauses for the specified number of the specified time units (default: seconds)
  • usleep <microseconds>  : Pauses for the specified number of microseconds
  • logout
  • shutdown  : Terminates Linux
  • halt  : Terminates Linux and shuts the computer down
  • reboot  : Terminates Linux and restarts the computer
  • lpr <filename>  : Sends the file to the printer queue
  • ldd <filename>  : Displays the libraries needed to run a program
  • set  : Displays all shell variables
  • printenv  : Displays only the environment variables
  • alias <name>='<value>'  : Defines an abbreviation
alias ls='ls -F'
  • unalias <name>  : Deletes an abbreviation
  • fsck [-a] <device>  : [Automatically] repair the file system
  • mke2fs  : Creates an ext2 file system
  • tune2fs -c <num-mounts> /dev/hda<n>  : Specify the number of mounts allowed before the ext2 filesystem on the specified device is checked again.
  • df -h  : New total / used / available space on all mounted partitions
  • du -s[b] <dir>  : View recursize directory size in KB [or in bytes]
  • free  : View memory usage
  • uname -r  : Find out the version number of the Linux kernel
  • sum <filename>  : Calculates the checksum for a file
  • cksum <filename>  : Calculates the CRC checksum for a file
  • md5sum <filename>  : Generate the md5 checksum for the specified file
  • touch <filename>  : Change the timestamp of the specified files to the current date and time
  • touch <filename> create file  : Create an empty file in the current directory
  • stat -c%y file.txt  : Date last modified
  • stat -c%s file.txt  : File size
  • basename <name> [<suffix>]  : Removes the directory path and optional filename suffix.
  • dirname <name>  : Removes the filename from the full path.
  • cp -dfuvp[r] <source> <dest>
  • mv -f <source> <dest>
  • mkdir -p <dest>
  • rm -fr <dest>   # Recursively delete files or directories
  • rmdir <dest>   # Only works if the directory is empty