Useful shell commands


BASIC COMMANDS

You can find a separate list of the basic commands here.

CONNECTIONS

Logging onto a CF machine from an RTDC one
ssh -X user@login.cfa.harvard.edu
ssh -Y user@mars
Run a command (hostname here) on a different machine
ssh user@host2 hostname
Allow x-windows to be displayed over SSH
ssh -Y user@host
Append a local file with a remote file over SSH
ssh user@rtdc7 ''cat myremotefile'' >> mylocalfile
Append a remote file with a local file over SSH
cat localfile | ssh user@rtdc8 ''cat >> remotefile''
Keep a process running after you log out
screen     [instructions here]

THE FILE SYSTEM

Search the /home/user directory for files ending in .sh
find /home/user -name '*sh'
Search the /home/user directory for files bigger than 10GB
find /home/user -size +10G
List all files excluding those ending in .gz
ls --hide='*gz'
List only items not starting with a number
ls -d [^0-9]*
Search for the word 'archive' in all files except those ending in .log
grep archive --exclude="*.log" *

VIEWING/MANIPULATING FILES

Find out the format of a file (e.g. ascii/gzip/tar/png)
file file1
View two text files side-by-side
diff -y file1 file2
View first 5 lines of a file
head -5 file1
View last 20 lines of a file
tail -20 file1
Monitor a file being updated in real time (ctrl-c to exit)
tail -f file1.log
Add sequential line numbers to the start of each row
nl file1
List contents of a tar or tar.gz file
tar -tvf file.tar.gz
Remove adjacent duplicate lines
sort file1 | uniq
Find lines that dont match between two files
grep -vFxf file1 file2
Delete any lines that match a pattern.
(Use -i.bak to overwrite the original and make a backup)
sed -i.bak '/pattern/d' file1
Only keep lines with 29 characters
sed -r '/^.{,29}$/d' file1
Sort on column 2 with / as a delimiter
sort -t/ -k 2 file1
Join multiple pdf files together
gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=outfile.pdf file1.pdf file2.pdf file3.pdf
Join multiple text files together
cat file1 file2 file3 > newfile