Basic Linux commands
Contents
Basic Linux commands#
Listing files, changing directories, and making new directories#
Find out what directory you’re currently in
pwd
Go to your home directory
cd ~
Make a new subdirectory called data
mkdir data
Now go into the directory
cd data
Now go back to the directory above it
cd ..
List the files in your home directory
ls ~
List the files in the current directory with a bunch of extra information
ls -l
List just the names of the files, in a single column, with no extra information.
ls -A1
List just the text files, single column, no extra info, and store the list in a file called myTextFiles.txt
ls -A1 *txt > myTextFiles.txt
Display a list of files sorted by date, with the most recent first
ls -lt
Display a list of files sorted by date, with the oldest first
ls -ltr
Tip
Those letters that specify options after linux commands are called command flags
Copy/move/delete files#
Copy all the files whose filename contains the word data into the data directory
cp *data* data
Move all the files whose filename contains the word data into the data directory.
mv *data* data
Delete all the files containing the word data in the current directory. (Be careful with this! These files get deleted, bypassing the Trash/Recycle-bin)
rm *data*
Rename files#
Simple renaming of a file
mv crappyFilename.png usefulFilename.png
More complicated renaming of a file - prefix a string to a filename: Grab a list of all the files with the extension .png and then prefixing “mask_” to each one
for filename in *.png; do mv "$filename" "mask_$filename"; done;
More complicated renaming of a file - replace a part of all filenames with something else
for filename in *.png*; do mv "$filename" "${filename//maskSquare/maskCircle}"; done
Display a file#
Print a history of your commands to the screen
cat ~/.bash_history
One screen at a time…
cat ~/.bashrc | more
Just the top
head ~/.bashrc
Just the bottom
tail ~/.bashrc
The first 10 lines
head -n 10 ~/.bashrc
Combine (concatenate) two or more files and write the output to another file#
Combine two files and print the results to the screen#
cat file1.txt file2.txt
(conCATenate, get it?)
Combine all files with the word ‘data’ in the filename and which end on txt.#
cat *data*txt
Do the same and write the output to a new file#
cat *data* > allDat.txt
(if allDat.txt already exists, it will be overwritten) Caveat: what
happens if you try cat *data* > alldata.txt
Append the output to a file (i.e., add to the end of the file)#
cat *data* >> allData.txt
Concatenate files while getting rid of the first line#
If you want to concatenate files that contain a column header, combining them using cat
will cause your resulting file to have lots of headers instead of just 1, at the top. There’s an easy (though unintuitive) solution for it: use tail
with some options to print all but the first line.
tail -n +2 file1.txt
You can direct it to output using >
as with cat (or any bash command). To combine multiple files, add them as above, e.g.
tail -n +2 *data*txt > allDat.txt
You’ll notice that if you pass multiple files to tail
, it will print the filename at the start of every file. This will mess up your neatly formatted file. To suppress the filename add -q
, i.e.,
tail -q -n +2 *data*txt > allDat.txt
Use cat and tail together:#
Let’s see how we can grab the header from one of the data files (using head
) and concatenate it with the output of tail which allows us to exclude the headers from the other files:
(head -n 1 data/one_of_the_files_to_get_header.csv && tail -q -n +2 data/*data*csv) > all_dat.csv
Note
Make sure all your files end with a newline or the first line of the next file will continue from the last line of the previous file.
Some miscellanous tips#
Get help on a command#
To look up detailed specifications on a command, type:
man ls
Note
If you’re using Cmder on windows, man won’t work. Use ls --help | less
instead
You can scroll through the documentation using PgUp/PgDown or up and
down arrows. To exit, press q
For a quick refresher on the arguments
ls --help
There are many many many linux commands. Probably the best way to find one is
by Googling, e.g., google for how to zip a file in linux
or ls command flags
You can also try the apropos
command in the terminal. For example, try
apropos zip
Become a more proficient computer user#
Use command-tab (Mac), alt-tab (windows) to switch between programs.
Learn keyboard shortcuts: shortcuts for Cut, Copy, Past, Select-All, Find, undo/redo, should be second nature. Here’s a list of Mac shortcuts, and a list of Windows shortcuts.