Linux Tips

From Lupyan Lab
Jump to: navigation, search


Basic linux commands

Listing files, changing directories, and making new directories

Find out what directory you're currently in

cd ~
mkdir data
cd data
cd ..
ls ~
ls -l
ls -A1
ls -A1 *txt > myTextFiles.txt
ls -lt
ls -ltr
mkdir newDirectory

Copy/move/delete files

cp *data* data
mv *data* data
rm *data*

Rename files

mv crappyFilename.png usefulFilename.png
for filename in *.png; do mv "$filename" "mask_$filename"; done;
for filename in *.png*; do mv "$filename" "${filename//maskSquare/maskCircle}"; done

Display a file

cat ~/.bash_history
cat ~/.bashrc | more
head ~/.bashrc
tail ~/.bashrc
head -n 10 ~/.bashrc

Combine (concatenate) two or more files and write the output to another file

cat file1.txt file2.txt

(conCATenate, get it?)

cat *data*
cat *data* > allDat.txt

(if allDat.txt already exists, it will be overwritten) Caveat: what happens if you try cat *data* > alldata.txt

cat *data* >> allDat.txt

Some miscellenous tips

Get help on a command

man ls

You can scroll through the documentation using PgUp/PgDown or up and down arrows. To exit, press q

ls --help

You can also try the apropos command in the terminal. For example, try  apropos zip

Become a more proficient computer user

Navigating a command line efficiently

Ctrl-a moves to the start of the command
Ctrl-e moves to the end of the command
You can also use the 'Home' and 'End' keys! (perhaps the most underused keys on a keyboard
Esc-b (i.e., the Escape key followed by the b key) moves back one word
Esc-f moves forward one word
Ctrl-k will cut the command line from the cursor point forward and place it in a buffer (aka clipboard)
Ctrl-y will paste what is in the buffer after the current cursor position

type cd then a space then the first letter or two of the directory you want to go to. Press <tab>.  Magic.
Caveat: commands like ls and cd are little executable programs. The stuff that follows the command is an argument (information telling the program what you want it to do). As with all programs, you must put a space in between the program name and its arguments, so it's ls --help, not ls--help

explorer .

Count words/lines/characters in a file

wc ~/.bash_history
find . -name "*txt" -exec wc -l {} \;

Get a particular column from a file

 cut -f1 test.txt

This gets the first column from a file called test.txt which has several columns separated by a tab (cut defaults to a tab delimiter. You can specify your own with -d "delim"

ls -l | awk '{print $6,$7,$8}'

How would you use awk to get a list of word-counts for all the .txt files in your directory?

Send an output of one command to another command

For example, the grep command in its basic form searches through text and outputs a line that matches some search term. Recall that you created an alias for psych711 in your .bashrc file. Let's find it.

cat ~/.bashrc | grep psych711

This means send the output of cat ~/.bashrc (the contents of the .bashrc file) to the command grep which then tries to find "psych711" within that output.
Caveat: you can also just type grep "psych711" ~/.bashrc, but in some cases using the '|' is the only way to do what you need to do.

Personal tools
Download and Install
Notes for each class - will be updated ~week before each class.
Programming Exercises
Quick reference