Linux useful commands

Regular commands I use every day in Linux plus a few eclectic ones. Basically geared to the new user. Tips or thoughts, please let me know.

Man pages

In Linux there is a manual for just about anything. Learn about almost everything by “man command” in the terminal. (e.g. man ls). Or type “command –help” for a basic description. Also, many man pages also cover configuration files (man resolv.conf).

Basic Commands

  • Up key- Command History
  • Tab - Auto-completion, nice and handy for completing file-names, directory names, and commands.
  • Commands are in this form: command -arguments

Files ( + directories )

ls ( list ), -l ( long ), -a ( shows hidden )
cp ( copy )
mv ( move or rename ), mv filename1 filename2
rm ( remove ) Very dangerous to use as root. Use with caution. -r ( recursive ) -f ( force - needed to remove a link)

Wildcards to expand definitions:

  • * (matches any character), cp *.txt ~/Desktop
  • ? (matches any single character), cp file?.txt ~/Desktop
  • [characters] (Matches a range/set of characters), cp [a-n]*.txt ~/Desktop
  • >
  • [!characters] (Matches any character that is not a member of the set characters)

Directories

cd    ( change directory )
pwd ( print working directory )
mkdir ( mkdir )
  • A name followed by a / means it’s a directory, bash is pretty good about figuring out what you mean if don’t use it but some apps don’t. A safe syntax would be cd myfiles/

Command Output to Text (Standard Output)

ls /usr/bin > /home/user/Desktop/programs.txt

Add to an existing text file:

ls /sbin >> /home/user/Desktop/programs.txt

Pipes ( | )

  • Useful for using programs in conjunction with others
ls -l | less

Filters

Popular filters used after piping.

  • sort
  • uniq - removes duplicate lines of data
  • grep - returns the output of a specified pattern of characters
  • head, tail - outputs the first of last lines of output
  • tr - translates characters - can be used for upper/lower case conversion

Use grep to extract patterns from files

grep EE /etc/X11/xorg.conf

ls -all /dev | grep dvd

glxinfo | grep -i direct

Files and File Permissions

View file permissions

  • List the files in long view:
ls -l

-rwxr--r-- 1 user user 225444 2007-05-01 21:58 abc.pdf
| | | | | |
| | | world | group name
| | group owner name
| owner/user
directory?

r = read = 4
w = write = 2
x = executable = 1

Change File Permissions

owner = 4+2+1 = 7 , group = 4

The above file’s permissions numerically is 744 to change permissions of the above file:

chmod 755 abc.pdf

Change Ownership

chown user:group /home/user/document.txt

Lazy way of make a file executable ;)

chmod +x /usr/bin/gmailto

File Systems

Show all partitions and their types (may not work on all types)

sudo fdisk -l

Show partition used/available space:

df -h

See all file systems mounted:

cat /proc/mounts

Sort Directories by How much space they consume

du | sort -nr

Mount a Volume and add it in permanently:

  • The fstab file tells system of available disks/partitions and can automatically mount it at boot.
sudo mkdir /mnt/USB-Drive
sudo mount /dev/sda2 -t vfat -o rw /mnt/USB-Drive
  • types include hfsplus, vfat…

Enter in /etc/fstab:

/dev/sda2    /mnt/OSX          hfsplus ro,exec,auto,users    0      0
/dev/sda4 /mnt/Shared_Disk vfat users,auto,uid=1000,gid=100,umask=007 0 0
  • ro - read-only, rw - read-write, auto mounts filesystem on boot

Unmount all possible file systems:

umount -a

Check File Systems

  • Mounted file systems should be checked from the Installer CD/DVD or on boot.

Force file system check on next boot (won’t do it immediately):

sudo touch /forcefsck

Reboot immediately and check for errors:

sudo shutdown -Fr now

Change how often fsck runs at boot:

sudo tune2fs -c 30 /dev/hda

Check and mark bad blocks on damaged drives:

mke2fs -j -c /dev/hda3

Get UUID of devices:
At times the fstab file will require a device ID (UUID) be supplied.

ls /dev/disk/by-uuid -alh

Swap

Create swapfile

dd if=/dev/zero of=/swapfile bs=1024 count=2097152
  • Swap is recommended to be 1-1/2 to 2x the value of the RAM to use for hibernation.
  • 1 GB = 1024 MB = 1024 x 1024 kB = 1048576 kB = 1048576 kB x 1024 bytes/kB = 1,073,741,800 bytes10
mkswap /swapfile
swapon /swapfile

Add to /etc/fstab:

/swapfile              swap             swap     defaults

Controlling Swap

Turn off swap:
swapoff -a /swap

  • Swapiness is the input/output priority of swap. To measure the current value:
cat /proc/sys/vm/swappiness

To change the swap priority (higher value means more swapping):

sysctl vm.swappiness=10
  • values of 20 or lower are better for laptops.

File Compression

Pack:

tar gunzip:
tar cvpzf /AreaToSaveTo/yourcompressedfile.tgz –exclude=/this/folderorfile /CompressionStarts/Here
tar.bz2(tbz2) (block sorted, better compression):
tar -cvjf files.tar.bz2 fileorfolder
bzcat linux-2.6.XX.tar.bz2 | tar x

Unpack:

tar gunzip:
tar -xf file.tgz
tar xvjpf file.bz2 /startplace
tar.bz2(.tbz2):
bzcat file.tbz2 | tar file.tar

View contents of tar files:

tar tzvf name_of_file.tar.gz | less

unrar

unrar e file.part01.rar
  • Now works in conjunction with file roller.

Span Multiple Volumes

create:

tar -c -M --tape-length=2294900 --file=part1.tar too-large-archive.tgz

extract:

tar -x -M --file=part1.tar too-large-archive.tgz
  • At prompt specify new (n),
  • then specify volume name (e.g. n part2.tar)
  • tape-length is 1024 bytes measurement
    • or (1 computer kilo)

Or use “split” to break a large volume:split -b 2m largefile LF_

  • 2m = 2 megabytes LF is the prefix for new name
tar -cvj /full/path/to/mybigfile | split -b 650m

Put back together:

cat file* > newfile

Backup and Restore

Tar - From Install CD

cd /mnt/gentoo
tar -czpvf /mnt/gentoo/MacBook-Gentoo-Backup.tgz *

Rsync - Full Backup

rsync -avtp --delete --exclude=/home/user/somedir /source/dir /destination/dir
  • -a archive, -v verbose
  • -t preserve modification times, -p permissions
  • –delete removes destination file if has been removed from source
  • –links recreate symlinks
  • -z compress from source to destination - good for slow connections.
  • use “-a e ssh source name@hostname:dest” for ssh

Rsync - Incremental Backup
NEEDED? I think the above does Incremental too.rsync -b –backup-dir= combination

  • used for daily or every other day

Users

Add user

useradd -m -G adm,audio,cdrom,cdrw,cron,games,plugdev,portage,shutdown,usb,users,video,wheel -s /bin/bash user
  • Groups may vary some per distribution, this one is for Gentoo.
  • Some groups may not be available until installation is finished

Add/delete user to group

gpasswd -a user plugdev
gpasswd -d user plugdev

See what groups user belongs to

id

Remove user

userdel username

CD / DVD

Writing to CD/DVD with Rock-Ridge support

  • Rock-ridge support add Unix file extensions and attributes for iso9660 standard disks.
  • DVD are marked as 4.7GB capacity but thats just the marketing measure. In terms the computer understand the space on a DVD is 4.368 GB’s
  • 1 GB = 1048576 kB x 1024 bytes/kB
  • DVD +R at 4x or 8x for best performance

DVD

growisofs -Z /dev/dvd -lrJ -joliet-long /path/to/files
  • -Z means to start at the beginning of the dvd
  • -l allows long filenames (breaks DOS compatability)
  • -r Rock-ridge support
  • -J Add Joiliet support
  • -joliet-long - allows Joliet filenames to be 103 characters long instead of 64 - breaks joliet compatibility but works in most cases.

CD

mkisofs -o my.iso -lrJ /path/to/files
  • Then burn iso to CD.
  • Not sure if I can write directly to CD, from what I’ve seen it would seem that I can’t.

Blanking a Disk

  • If you want to blank a disk or it already has a file-system on it you’ll see an error like “WARNING: /dev/hda already carries isofs!” then reinitialize the filesystem:

DVD

dvd+rw-format -f /dev/dvd
growisofs -Z /dev/hda=/dev/zero

CD

cdrecord -v dev=/dev/hda blank=fast
cdrecord -v dev=/dev/hda speed=2 blank=fast
cdrecord -vv dev=1,0 blank=all

ISO

Write ISO to CD/Drive:

dd if=name.iso of=/dev/sdb1

Mount ISO:

mount -t iso9660 -o loop,ro name.iso /mnt/cdrom/
mount /path/to/name.iso /mnt/cdrom -t iso9660 -o ro,loop=/dev/loop0

Create an ISO from a DVD or CD:

dd if=/dev/hda of=name.iso

Create and ISO from a file/directory:

mkisofs -o name.iso /path/to/file_or_directory

CDRWin (.bin/.cue) images to .iso:

bchunk name.bin name.cue name.iso
bin2iso name.cue

Converting CloneCD images to ISO:

ccd2iso name.img name.iso

Converting nrg (Nero) images to ISO:

nrg2iso name.nrg name.iso

Support for writting large file sizes

  • ISO has file size limit of 4GB
  • untested - udf support is still in alpha
mkisofs -o my.iso -lrJ -allow-limited-size -udf file-or-pathtofiles
growisofo -Z /dev/dvd -lrJ -allow-limited-size -udf file-or-pathtofiles

Mouse/Keyboard

Change keymaps:

setxkbmap dvorak

Map pointer buttons to keyboard:

xmodmap -e 'keycode 116 = Pointer_Button2'
xmodmap -e 'keycode 108 = Pointer_Button3'
xkbset exp m

Hardware Info

Kernel messages about hardware

dmesg | less

Cpu info:

cat /proc/cpuinfo

List all PCI devices

lspci

Detect hardware as it’s plugged in

tail -f /var/log/messages

For more detail

lshal --monitor

Icons / Cursors / Fonts …

Reset Icon Cache

gtk-update-icon-cache -f /usr/share/icons/hicolor/

Convert Windows Icons to Linux

Reset cache for fonts:

fc-cache -vf

Build font info per directory:

mkfontscale
mkfontdir

Replace fonts script

cd /etc/fonts/conf.d/ && ln -sf ../conf.avail/61-replace-corefonts.

Take screenshot of selected area

import filename.png

Set gamma

  • If you have ability to calibrate your own icc profile ( Macintosh’s do ) copy the icc profile to Linux and use “xcalib icc.profile”, otherwise a basic gamma can be set:
xgamma -bgamma 0.925 -ggamma 0.925 -rgamma 0.925

System

Shutdown at a specific time

shutdown -h 22:33

Manually Shutdown

shutdown -P now

date
use “date” to check date and to set system clock:

date MonthDayHourMinuteYear

Find out kernel version:

uname -r

Start Program that isn’t in the Systems Path

  • Only programs that are in a system’s PATH setting can be started by typing the command
./program

Disable Touchpad whilest Typing

syndaemon -d -t -i 2

Networking

Samba

Change or add password to smbconf:

sudo smbpasswd -L -a user

Mount SMB share to folder

sudo smbmount //192.168.1.105/user/ mnt/directory -o username=username,password=pass,uid=1000,mask=000

Mount all Samba Shares in fstab

mount -a -t smbfs

SSH / SCP

Remote login with ssh with username (diiferent that the one you’re using)

ssh -l username 192.168.1.101

Copy remote file to local file

scp -p user@192.168.1.101:~/Desktop/file.name file.name

Download entire website:

wget -r http://www.robot-frog.com/

Advanced

Bash

The ~/.bashrc file

  • Adding PATHs to the ~/.bashrc file will make the system aware of another folder that has executables.
  • Shortcuts can be created for common commands
export PATH=$PATH:/home/user/.scripts
alias capscreen="import ~/Desktop/screen.png"

Analyze Bash History

cat .bash_history | tr '|' '\n' | awk '{print $1}' | \
egrep -o '([^/]+)$’ | sort | uniq -c | sort -nr | awk ‘{print $2 “,” $1}’

To see the preset variables already defined for bash:

set

Search History

ctrl-r

Cron

  • Cron is the system timer. It checks every minute for commands to run.

To edit a crontab (cron jobs)

crontab -e

# minute (0-59),
# | hour (0-23),
# | | day of the month (1-31),
# | | | month of the year (1-12),
# | | | | day of the week (0-6 with 0=Sunday).
# | | | | | user
# | | | | | | commands
43  08  *   *   *       env DISPLAY=:0.0 audacious [[/home/user]] /My\ Music/Other/Alarms/301gq.mp3

chroot - (changing root)

  • Userful for logging into your current Linux from an installtion CD
su
mkdir /mnt/osname
mount /dev/sda3 /mnt/osname
mount -t proc none /mnt/osname/proc
mount -o bind /dev /mnt/osname/dev
chroot /mnt/osname /bin/bash

Compile Kernel

make oldconfig
make menuconfig
make clean zImage modules modules_install install

For PPC “make pmac32_defconfig” will generate a basic config.

Find Modules

find /lib/modules/2.6.20-gentoo-r2-ibook-SE-g3 -type f -iname ‘*.o’ -or -iname ‘*.ko’

Add the screen program to be able to background a terminal process
screen command

  • CTRL + A + D to background it, to return it:screen -r

Use noup to continue a process even if you log out
noup command

Unsorted / Less Used

sudo echo >> no work

echo "my text" | tee /etc/portage/package.use

See whats taking up ram:

ps auxf --sort size

Generate Modelines for xorg.conf

gtf screenwidth screenheight vertrefresh

Reset settings in gconf:

gconftool-2 --recursive-unset /apps/compiz 

GDM conf file

Touch Entire System
(careful I’ve done this before when my files got dated wrong from a dead battery. But I tried again and the files are put where the command is executed - all 0 bytes)

find /| xargs touch - m

Allow window executables to run directly (will need Wine and misc. binaries enabled in kernel)
In /etc/sysctl.conf add

fs.binfmt_misc.register = :WINEXE:M::MZ::/usr/bin/cedega:

and add to fstab

none  /proc/sys/fs/binfmt_misc  binfmt_misc  defaults 0 0

Re-size Images

  • requires imagemagick
convert writes new image, mogrify overwritesconvert image.jpg --resize 800x600 newresized.pngmogrify -geometry 1024x768 *.png

Copy ALL Files (+invisible, hard links, softlinks)

find . -depth -print0 | cpio –null –sparse -pvd /mnt/newhome/

Create random numbers, hex letters

dd if=/dev/random bs=1 count=5 2>/dev/null | xxd -ps

Run programs sequentially or concurrently

program && program
program & program

Hardware acceleration enabled?

glxinfo | grep rendering

A simple web server

Share files in directory and all subfolders:

python -c "import SimpleHTTPServer;SimpleHTTPServer.test()"

View in:

http://localhost:8000 or http://your_ip:8000/

Debian Specific:

Run Program as normal user:

sudo dpkg-statoverride --update --add root root 4755 /usr/share/app

drive space show taken by installed packages

dpkg-query -W --showformat='${Installed-Size;10}\t${Package}\n' | sort -k1,1n

Rebuild Font Directory

dpkg-reconfigure fontconfig

0 comentarios: