This page contains a selection of tips for Linux (and UNIX) that I have collected. It's more of a memo to myself and not intended as a guide or anything. So just see it as notes that I share with whoever is interested. Most of these tips are for the bash shell environment.
replace "str1" by "str2" in previous command:
^str1^str2[^]
repeat event from 2 events ago:
!-2
repeat most recent event starting with "string":
!string
repeat most recent event containing "string":
!?string[?]
repeat the entire command line typed so far:
!#
Directory listing including non-printable chars
ls -q *
list directory names not their contents
ls -d *
List all entries in current directory that are files (not directories),
alternatively use find
with the -depth
option.
for i in *; do [ -f $i ] && echo $i; done
And inversely list only directories:
for i in *; do [ -d $i ] && echo $i; done
Push current directory to directory stack:
pushd .
Pop directory from directory stack:
popd
Information about the system:
uname -a
How many cores does the system have:
grep "model name" /proc/cpuinfo | wc -l
Viewing file with line numbers:
cat -n filename
"more" file starting at a certain linenumber:
more +linenumber filename
"more" file starting at a certain pattern:
more +/pattern filename
Rename all .htm files to .html in one go:
rename .htm .html *.htm
Destroy a file:
shred -uz filename
Mail a file as plain text:
mail -s "subject" e-mail_address < file
Mail an attachement (if uuencode present and in path):
uuencode attachment.txt | mail -s "subject" e-mail_address
Do some quick maths (whole numbers only):
echo $((7*8+5))
To sort based on a section of each line in a file use the --key argument. For instance to list countries in alphabetical order of name rather than their country code:
grep -v "^#" /usr/share/zoneinfo/iso3166.tab |sort --key=2.1
Display current processes
ps -ef|cut -c49-
Display the processes of user userid
ps -fu userid
Show the processes started on tty
ps -ft tty
Show the process with PID pid
ps -fp pid
Restarting a process with PID pid:
kill -HUP pid
Restarting sshd:
kill -s HUP $(cat /var/run/sshd.pid)
Editing ssh settings:
vi /etc/ssh/sshd_config
Changing (lowering) a running process' priority:
renice +10 -p pid
Commands you can use in an FTP session.
File permissions on target site:
site mask 133
site chmod 644 filename
Toggle interactive prompting of multiple arguments commands:
prompt
Status of current connection:
status
Setting up passwordless login for sftp and ssh:
ssh-keygen -t rsa
ssh-copy-id -i ~/.ssh/id_rsa.pub username@remote_host
sftp username@remote_host
Checking the last logins of the current user:
last -i | grep $USER
Last logins of all users on a machine:
last -i
Run one instruction as user id:
su - id -c "instruction"
Display terminal settings:
stty -a
Display configuration of system variable:
getconf CHILD_MAX
Set tab-stop interval to 4 in the terminal:
tabs -4
tr also works with these kind of constructions:
tr '[:lower:]' '[:upper:]'Forgetting the 's (single quotes) will cause problems if there is a file in your current directory with the name: l, o, w, e, r, u, p or : Try
touch u && echo tr [:lower:] [:upper:]
To remove CRs (^M) from ascii files ftp'd in binary mode from Windows to Linux. An EOF (^Z) remains at the end of the file.
tr -d '\r' < file_with_CR > file_without_CR
Extract a single file from tar archive into the current directory, whitout needing to know the full path (obviously only works if the filename is unique):
tar -xf tarfile --transform='s,.*/,,' $(tar -tf tarfile | grep filename)
You need to be root to run these instructions:
Status of all services:
service --status-all
Restart a service (for example the network service) (similar with start or stop):
service network --full-restart
List all services settings for all runlevels:
chkconfig --list
Example: turn mysql deamon on for runlevels 2 to 5:
chkconfig mysqld on
With systemd
Status of all daemons:
systemctl
Restart the http daemon (stop and start also work):
systemctl restart httpd.service
Turn the http daemon on in default runlevel:
systemctl enable httpd.service
Stopping and starting gnome:
gdm-stop
gdm-start
Run yum instructions as root:
Clean up packages downloaded by yum:
yum clean packages
Exclude a package from being updated, handy if a package is causing a missing dependency error:
yum update --exclude=package
Example to update everything except the kernel:
yum update --exclude=kernel*
Search for a package:
yum search word
Make a screenshot of the window in focus in 5 seconds:
gnome-screenshot --window --delay=5
Flush the Gnome keyring passphrases from memory
gnome-keyring-daemon -r -d
Turn off screensaver and powersave mode (can also use dconf-editor
):
gsettings set org.gnome.desktop.screensaver idle-activation-enabled false
gsettings set org.gnome.settings-daemon.plugins.power sleep-display-ac 0
DO NOT "set org.gnome.settings-daemon.plugins.power active false" this removes the battery icon.
ffmpeg can be used for all video and audio formats, these examples just use one as an example.
Convert a .mov file to a .flv file (-s optional resize):
ffmpeg -i input_filename.mov -s widthxheight output_filename.flv
Cut a section from an MP3 sound file (-t = total length of clip):
ffmpeg -i input_filename.mp3 -ss HH:MM:SS -t HH:MM:SS output_filename.mp3
Rotate a video clip:
mencoder -vf rotate=1 -ovc lavc -oac copy -o output.avi input.avi
Rotate right: rotate=1, rotate left: rotate=2
Extract sound from a .WebM video clip:
ffmpeg -i input_filename.webm -vn -acodec copy output_filename.ogg
Extract mp3 from an mp4 video clip:
ffmpeg -i input_filename.mp4 -vn output_filename.mp3
Remove sound from a video file:
ffmpeg -i input_filename.webm -an output_filename.webm
Convert an image to a PDF document:
convert imagefile docname.pdf
You need to have imagemagick
installed for the
convert
utility.
To create a new user from the command line, log in as root
adduser username
passwd username
Look up the definition of a word (in English) from the command line:
curl dict://dict.org/d:word
7is7.com | Software | Otto | Travel Stories | Countdown Clock | Firefox | StatEye
New | About | Contact | Connect | Friends | Promotions | Copyright | Advertise