Other stuff‎ > ‎

Unix/Linux notes


How to make a tar archive

tar -cvzf name-archive.tgz name

"name" is the name of the folder we want to save as tar archive, "name-archive" can be anything.

How to list the content of a tar archive

c option tells tar to create an archive;
v option tells it to write out the names of the files on your screen as it saves them, to be sure it is including everything;
z option specifies that the file should be compressed, to save space;
f option instructs tar to use the next name (name-archive in the example above) for the tarball it is building.


tar -tvzf filename

How to expand a tar archive

tar -xvzf filename

Chmod

0 (or blank) = No permissions
1 = Execute
2 = Write
3 = Execute + Write
4 = Read
5 = Read + Execute
6 = Read + Write
7 = Read + Write + Execute

Using the command: chmod ABC file_name (wherre ABC being: A permissions for 'owner', B permissions for 'group', C permissions for 'other'.

For example chmod 764 file_name does the following: 
'owner' of the file can read, write and execute; 
the 'group' can read and write;
'other's can only read.

Changing names of many files

method 1 (zsh)

autoload -U zmv
alias mmv='noglob zmv -W'

Then:

mmv 1105\ A*.osc A*.osc

Also a good idea is to put alias mmv='noglob zmv -W' in .zshrc file

method 2 (perl script)

chmod a+x it and then put it in your path (~/bin)

You run it like this:

rename-alot 's/1105 A/A/' *.osc

The above will change all files that look like this: "1105 A0176.osc" to this "A0176.osc"

The script:

#!/usr/bin/perl -w
# rename - Larry's filename fixer
$op = shift or die "Usage: rename-alot expr [files]\n" .
"eg: % rename-alot \'s/abc /abc-/\' abc*txt\n" ;
chomp(@ARGV = <STDIN>) unless @ARGV;
for (@ARGV) {
$was = $_;
eval $op;
die $@ if $@;
rename($was,$_) unless $was eq $_;
}

method 3 (linux command 'rename')

rename 1055 '' 1055*.osc

method 4

To remane 336 images files from 760D6a0001(to 336).img to 760D6a0_001(to _336).img
Just save the script below (like filename.com) and run as sh filename.com:

for i in 760D6a0***.img
do
mv $i ${i%760D6a0***}760D6a0_${i#760D6a0}
done

VIM

- Basic commands:

Substitutions (find and replace):

vi junk
:1,$s/pinco/pallino/g

The control-V operation:

Cntr-V

select a block, then use:
d
to remove it, or
p or P
to paste it.

Control-V and Insert or Replace

- Ctrl + V to go into column mode
- Select the columns and rows
-
Shift + i to go into insert mode in column mode
- Type in the text you want to enter.
- Esc to apply your change (or alternately Ctrl+c)
  
  Changes will show up only after hitting Esc

Same as above to replace text. First insert modification desired, then delete (always in column mode)?
No, better this way:

- "Ctrl+v" to go into column mode
- "Shift+g" to go to the last line, move the cursor to the original column again (as g moves the cursor to the first non-blank position)
- Type "c" to remove eveything and go into INSERT mode
- Insert desired substitution
- Hit "esc" twice to exit from column mode and see the changes

The command "find"

find . -name "*.rgb" -exec ls -l {} \;

(to print out a list of all the RGB images in your home area)

find . -name "*.ps" -exec ls -l {} \;

(to print out a list of all the PostScript files in your home area)

Use the following command to automatically 'gzip' files (in these examples rgb and ps files):

find . -name "*.rgb" -exec gzip {} \;
find . -name "*.ps" -exec gzip {} \;
find . -name "*.rgb" -exec ls -l {} \;

The command must have \; at the end (generally for an exec command), {} is substituted by the name of a specifications, -exec allows to perform a command on any files that match the 'find' specifications - here, the command is 'ls -l' followed by a filename, using 'ls -l' allows you to see the full pathname of any found files, find any files whose filenames match "*.rgb", the '.' after find start the search from the current working directory.


How/where to find a file

Go to the top directory (~) and type:

find . -name "*" -print

where * is the name we are looking for.

LS keywords

to find the biggest file:

ls -al

better this one:

ls -al | sort -r -k5 | head!ls color

Edit ~/.bash_profile (or /etc/zshenv if using zsh) and add the following two lines:

export CLICOLOR=1
export LSCOLORS=~ExFxCxDxBxegedabagacad


Change 'ls' directory colors

Use export LS_COLORS command. 
To make all directories yellow, add this to ".bash_profile" in the home directory:

export ~LS_COLORS='di=01;33'

Other good colors for directories: bold white (01;37) and bold purple (01;35).

LOWER PRIORITY of a job AND PUT IN BACKGROUND

(in order to run other programs in the meanwhile):

npri -w cns_solve < 2fofc_map.inp > 2fofc_map.out

IN ORDER TO START THE RUN OF A PROGRAM AFTER 4000 SECONDS THAT THE FIRST PROGRAM HAS STARTED-AND HENCE FINISHED BEFORE 4000 SEC-PUT THE SECOND RUN TO SLEEP FOR 4000 SEC AND AFTER THOSE IT WILL START BY ITSELF:
(type man sleep for the manual)

sleep 4000 ; cns < composite_omit_map.inp > composite_omit_map.out &

To change permission to allow WRITE into a directory

chmod o+w 

and add name of the DIRECTORY, so it goes from drwxr-xr-x to drwxr-xrwx

COUNT LINES IN FILE

grep 'CA ' temp.pdb | wc -l

To enable the use of display (X) from remote machine

First set the xhost using xhost + 'name of the remote machine' (xhost + em2.tata.tatata in any terminal of the machine you're using)
i.e. from em@tutu.tututu:

em@tutu.tututu:~/ > xhost + em2.tata.tatata
em2.tata.tatata being added to access control list

Login using ssh in the remote machine (that is em2.tata.tatata) and type:

setenv DISPLAY em@tutu.tututu:0

Search for a character string then output neighboring lines

GNU grep does the job, it has -A <num>, -B <num>, and -<num> options to print <num> lines of trailing, preceding, or surrounding context.