7is7.com > Software > Linux Tips
Linux Penguin

Linux Tips

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.

Manipulating command line instructions

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 Contents

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

Directory Manipulation

Push current directory to directory stack:

pushd .

Pop directory from directory stack:

popd

System

Information about the system:

uname -a

How many cores does the system have:

grep "model name" /proc/cpuinfo | wc -l

Files

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

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

Arithmetics

Do some quick maths:

echo $((7*8+5))

Processes

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

ftp

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

sftp and ssh

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

misc

Run one instruction as user id:

su - id -c "instruction"

Display terminal settings:

stty -a

Display configuration of system variable:

getconf CHILD_MAX

tr

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

tar

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)

services / daemons

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

yum

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

Gnome Tricks

Make a screenshot of the window in focus in 5 seconds:

gnome-screenshot --window --delay=5

FFmpeg and mencoder (Media Conversions)

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

Alsamixer settings

Set and save as root (requires alsa-utils package)

alsamixer -c0
alsactl store

Internet based services

Look up the definition of a word (in English) from the command line:

curl dict://dict.org/d:word

 


Linux user 425492

Get Firefox! Download LibreOffice Use Fedora