Archive

Archive for the ‘UNIX’ Category

20 Command line shortcut tips in Linux

September 15, 2011 Leave a comment

Command line shortcuts in Linux for command edit options and other activities.

  • Ctrl + a => Return to the start of the command you’re typing
  • Ctrl + e => Go to the end of the command you’re typing
  • Ctrl + u => Cut everything before the cursor to a special clipboard
  • Ctrl + k => Cut everything after the cursor to a special clipboard
  • Ctrl + y => Paste from the special clipboard that Ctrl + u and Ctrl + k save their data to
  • Ctrl + t => Swap the two characters before the cursor (you can actually use this to transport a character from the left to the right, try it!)
  • Ctrl + w => Delete the word / argument left of the cursor
  • Ctrl + l => Clear the screen
  • tty => Typing the tty command tells you what virtual terminal you’re currently working in.
  • Shift + PageUp => Scroll terminal output up.
  • Shift + PageDown => Scroll terminal output down.
  • Ctrl + l => Does exactly the same as typing the clear command.
  • Reset => If you mess up your terminal, use the reset command. For example, if you try to cat a binary file, the terminal starts showing weird characters. Note that you may not be able to see the command when you’re typing it.
  • Ctrl + r => Find the last command that contained the letters you’re typing. For example, if you want to find out the last action you did to a file called “file42.txt”, you’ll press Ctrl + r and start typing the file name. Or, if you want to find out the last parameters you gave to the “cp” command, you’ll press Ctrl + r and type in “cp”.
  • Ctrl + c => Kill the current process.
  • Ctrl + z => Send the current process to background. This is useful if you have a program running, and you need the terminal for awhile but don’t want to exit the program completely. Then just send it to background with Ctrl+z, do whatever you want, and type the command fg to get the process back.
  • Ctrl + d => Log out from the current terminal. If you use this in a terminal emulator under X, this usually shuts down the terminal emulator after logging you out.
  • Ctrl + Alt + Del => Reboot the system. You can change this behavior by editing /etc/inittab if you want the system to shut down instead of rebooting.
  • Ctrl + Alt + Backspace => Reboot the Graphics.
Categories: UNIX

Useful UNIX Tips and Tricks

September 7, 2011 Leave a comment

Here are a few pieces of Unix wisdom, I picked up during my 10 years of experience…

You’ve done something that has completely screwed up your terminal. Everything you type is either invisible or incomprehensible. Try the following:

$ ^C

$ stty sane^J

If this doesn’t work, try:

$ echo ^V^O

To determine what program dumped an anonymous core file, use gdb:

$ gdb -core core

Core was generated by `a.out’.

If you are on a system without gdb, try the file command:

$ file core

core: ELF 32-bit LSB core file of ‘a.out’ (signal 6)

Some keyboard shortcuts for editing

There are some pretty useful keyboard shortcuts for editing in bash. They might appear familiar to Emacs users:

Ctrl + a => Return to the start of the command you’re typing

Ctrl + e => Go to the end of the command you’re typing

Ctrl + u => Cut everything before the cursor to a special clipboard

Ctrl + k => Cut everything after the cursor to a special clipboard

Ctrl + y => Paste from the special clipboard that Ctrl + u and Ctrl + k save their data to

Ctrl + t => Swap the two characters before the cursor (you can actually use this to transport a character from the left to the right, try it!)

Ctrl + w => Delete the word / argument left of the cursor

Ctrl + l => Clear the screen

To create a vi macro that will wrap the current paragraph to a reasonable number of characters, add the following to your .exrc or .vimrc file:

map Q {!} fmt -c -u^M

Now, when you press ‘Q’ in visual mode, the current paragraph will be wrapped to approximately 70 characters.

When recursive copying, cp (cp -Rip, etc.) may not be the best tool for the job. For example, cp copies hard links as separate files, which is probably not what you want. To get a true copy of a directory, try:

$ tar cf – <dir> | (cd <destdir>; tar xf -)

This will create an exact copy of ‘dir’ in ‘destdir’. The same principle can be used to create a recursive copy on a remote machine:

$ tar cf – <dir> | ssh remotehost “(cd <destdir>; tar xf -)”

To list the files in another directory that match more than one pattern, it is easiest to do:

$ ls -l /usr/local/foo/{*.conf,*.local,*.rc}

which is equivalent to:

$ ls -l /usr/local/foo/*.conf /usr/local/foo/*.local /usr/local/foo/*.rc

This syntax is supported by (at least) bash, ksh, csh and sh.

You can extend this idea to make renaming files in another directory, for example, a little easier:

$ mv -i /usr/local/foo/bar/baz/{stuff,stuff~}

A little insurance against running ‘rm -rf *’ in the wrong directory — create an empty file called -i in any critical directory:

$ >-i

or…

$ touch — -i

If the ‘rm -rf *’ command is issued in that directory, the shell will expand the ‘-i’ early on and go into interactive mode, thus giving you a chance to say ‘Whoa, that was close!’, which always sounds better than ‘Oh fsck!’.

This tip works because the -i option to rm will override any previous -f.

Be forewarned that this tip only protects against ‘rm -fr *’, i.e., files and directories in the current directory.

To remove the -i file (or any other file beginning with ‘-‘):

$ rm ./-i

or…

$ rm — -i

To return to the previous directory in ksh or bash, use:

$ cd –

To use the last argument of the previous command line as an argument, use $_. For example:

$ ls -l /usr/home/dmr/somefile

$ vi $_

To make the up and down arrow keys work and thereby enable command line editing and recall in ksh, include the following lines in your ~/.profile:

set -o emacs

alias __A=’^P’

alias __D=’^B’

alias __B=’^N’

alias __C=’^F’

alias __H=’^A’

You need to enter the actual control characters ^P, ^B, etc.

You can kill any process with ‘kill -9’ as root, right? Not necessarily. It’s quite easy to write a process that cannot be terminated, even with ‘kill -9’.

This was a hard-learned lesson that will only be given out via email request, if at all.

Categories: UNIX

File Types – Permissions – Symlinks

###File Types – Permissions – Symlinks###

Features:

1. The ability to restrict/control access to files

Note: 10 bits represent permissions for files (including directories)

Note: use ‘ls -l’ to examine permissions or GUI application like ‘Nautilus’

-rwxrwxr-x 1 linuxcbt linuxcbt 681 Jan 13 11:31 regextest.pl

1st bit = file type. ‘-‘ = file, ‘d’ = directory

2nd – 4th bits = owner’s permissions

r = read = 4

w = write = 2

x = execute = 1

– = none = 0

5th – 7th bits = group owner’s permissions

r = read = 4

w = write = 2

x = execute = 1

– = none = 0

8th – 10th bits = everyone (world)

r = read = 4

w = write = 2

x = execute = 1

– = none = 0

Task:

1. Manipulate file permissions using ‘chmod’

a. chmod -x regextest.pl

-rw-rw-r– 1 linuxcbt linuxcbt 681 Jan 13 11:31 regextest.pl

rw = 6 or 4+2 for owner

rw = 6 or 4+2 for group owner

r = 4 for everyone else (world)

Octal notation: 664 for file ‘regexetest.pl’

chmod 664 regextest.pl – removes execution for ALL users

chmod 775 regextest.pl – enables execution for ALL users

2. Ensure that ‘regextest.pl’ is rw by owner and noone else

a. chmod 600 regextest.pl

Note: File will now be rw by owner (linuxcbt) and ‘root’

3. Ensure that ‘regextest.pl’ is r by owner and noone else

a. chmod 400 regextest.pl && ls -l regextest.pl

Note: chmod supports string values, which represent octal values

chmod +/- x file

chmod +/- w file

chmod +/- r file

chmod +/- u+x file – updates owner’s execute permissions on the file

chmod +/- o+x file – updates other’s execute permissions on the file

chmod +/- g+x file – updates group’s execute permissions on the file

chmod a+rwx = chmod 777

chown – permits changing of ownership of files

a. chown root regextest.pl – changes ownership to ‘root’

b. chown linuxcbt:sales regextest.pl – changes owner and group to ‘linuxcbt:sales’

Task:

Update ‘regextest.pl’ so that owner and group owner may modify the file

a. chmod 660 regextest.pl

SETUID:

Features:

1. ability to execute file as owner

chmod 4760 regextest.pl – this will ensure that the perl script always executes as the user ‘linuxcbt’

-rwsrw—- 1 linuxcbt sales 787 Jan 13 16:08 regextest.pl

‘s’ in the execute position means that the program will execute as that user

SETGID:

Features:

1. Ability to enforce permissions to a directory structure

mkdir /sales

chmod 2775 /sales

Create a file in the ‘/sales’ directory as ‘linuxcbt’

seq 1000000 > linuxcbt.1million.txt

chgrp:

Permits updating of group permissions

Sticky Bit:

Features:

1. Ability to ensure that users cannot delete others’ files in a directory

drwxrwxrwt 23 root root 4096 Jan 13 15:05 /tmp/

/tmp – users cannot delete other user’s files in ‘/tmp’

chmod 3777 /sales – ensures that /sales will not lose files from incorrect users

Task:

1. Set ‘/sales’ using sticky bit and test

a. chmod 3777 /sales && ls -ld /sales OR chmod 777 /sales && chmod +t /sales

Categories: UNIX

Linux utilities: GREP

September 21, 2010 Leave a comment

Introduction to GREP

grep, egrep, fgrep – prints line matching pattern (regular expression)

Features:

1. The ability to parse lines based on text and/or RegExes

2. Post-processor

3. Searches case-sensitively, by default

4. Searches for the text anywhere on the line

Examples:

1. grep ‘Linux’ grep1.txt – normal search – searches Linux in grep1.txt and not linux

2. grep -i ‘linux’ grep1.txt – case-insensitive search search

3. grep ‘^linux’ grep1.txt – uses ‘^’ to anchor searches at the beginning of lines

4. grep -i ‘linux$’ grep1.txt – uses ‘$’ to anchor searches at the end of lines

Note: Anchors are RegEx characters (meta-characters). They’re used to match at the beginning and end of lines

5. grep -a ‘myfuncion’ binfile – searches binary file as if it is a text file

6. grep ‘[0-9]’ grep1.txt – returns lines containing at least 1 number

7. grep ‘[a-z]’ grep1.txt – returns lines containing at least 1 alphabet

8. rpm -qa | grep grep – searches the package database for programs named ‘grep’

9. rpm -qa | grep -i xorg 􀀀 wc -l – returns the number of pacakges with ‘xorg’ in their names

10. grep -c ‘name’ myfile1 myfile2 – returns the count of matching lines in each file

11. grep -v sshd messages – performs and inverted search (all but ‘sshd’ entries will be returned)

13. grep -C 2 sshd messages – returns 2 lines, above and below matching line

14 grep -o ‘matchtext’ myfile – returns only the matching part and not the complete line.

Note: Most, if not all, Linux programs log linearly, which means one line after another, from the earliest to the current

Note: Use single or double quotes to specify RegExes

Also, execute ‘grep’ using ‘egrep’ when RegExes are being used

Categories: UNIX Tags:

basic linux commands

September 20, 2010 Leave a comment

Basic Linux commands used in day 2 day activities:

1. tty – reveals the current terminal

2. whoami – reveals the currently logged-in user

3. which – reveals where in the search path a program is located

4. type

5. echo – prints to the screen

a. echo $PATH – dumps the current path to STDOUT

b. echo $PWD – dumps ths contents of the $PWD variable

c. echo $OLDPWD – dumps the most recently visited directory

6. set – prints and optionally sets shell variables

7. clear – clears the screen or terminal

8. reset – resets the screen buffer

9. history – reveals your command history

a. !690 – executes the 690th command in our history

b. command history is maintained on a per-user basis via:

i. ~/.bash_history

ii. ~ = users’s $HOME directory in the BASH shell

10. pwd – prints the working directory

11. cd – changes directory to desired directory

a. ‘cd ‘ with no options changes to the $HOME directory

b. ‘cd ~’ changes to the $HOME directory

c. ‘cd /’ changes to the root of the file system

d. ‘cd Desktop/’ changes us to the relative directory ‘Desktop’

e. ‘cd ..’ changes us one-level up in the directory tree

f. ‘cd ../..’ changes us two-levels up in the directory tree

12. Arrow keys (up and down) navigates through your command history

13. BASH supports tab completion:

a. type unique characters in the command and press ‘Tab’ key

14. You can copy and paste in GNOME terminal windows using:

a. left button to block

b. right button to paste OR Ctrl-Shift-v to paste

15. ls – lists files and directories

a. ls / – lists the contents of the ‘/’ mount point

b. ls -l – lists the contents of a directory in long format: Includes permissions, links, ownership, size, date, name

c. ls -ld /etc – lists properties of the directory ‘/etc’, NOT the contents of ‘/etc’

d. ls -ltr – sorts chronologically from older to newer (bottom)

e. ls –help – returns possible usage information

f. ls -a – reveals hidden files. e.g. ‘.bash_history’

Note: files/directories prefixed with ‘.’ are hidden. e.g. ‘.bash_history’

16. cat – concatenates files

a. cat 123.txt – dumps the contents of ‘123.txt’ to STDOUT

b. cat 123.txt 456.txt dumps both files to STDOUT

c. cat 123.txt 456.txt > 123456.txt – creates new concatenated file

17. mkdir – creates a new directory

a. mkdir testRH5 – creates a ‘testRH5’ directory

18. cp – copies files

a. cp 123.txt testRH5/

By default, ‘cp’ does NOT preserve the original modification time

b. cp -v 456.txt testRH5/

19. mv – moves files

a. mv 123456.txt testRH5/ – moves the file, preserving timestamp

20. rm – removes files/directories

a. rm 123.txt

b. rm -rf 456.txt – removes recursively and enforces

21. file – outputs the filetype in detail

22. touch – creates blank file/updates timestamp

a. touch test.txt – will create a zero-byte file, if it doesn’t exist

b. touch 123456.txt – will update the timestamp

c. touch -t 200801091530 123456.txt – changes timestamp

23. stat – reveals statistics of files

a. stat 123456.txt – reveals full attributes of the file

24. find – finds files using search patterns

a. find / -name ‘fstab’

Note: ‘find’ can search for fields returned by the ‘stat’ command

25. alias – returns/sets aliases for commands

a. alias – dumps current aliases

b. alias copy=’cp -v’

Categories: UNIX Tags: