7is7.com > Software > Linux Tips
Linux Penguin

Linux Tips

This pages 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

misc

Information about the system:

uname -a

Run one instruction as user id:

su - id -c "instruction"

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:

site mask 133
site chmod 644 filename
prompt = toggle interactive prompting of multiple commands
status = status of current connection

sftp

Setting up passwordless login for sftp

ssh-keygen -t rsa
ssh-copy-id -i ~/.ssh/id_rsa.pub username@remote_host
sftp username@remote_host

misc (part 2)

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

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

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

Gnome Tricks

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

gnome-screenshot --window --delay=5

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! Use OpenOffice.org Use Fedora