UNIX Command Line Usage Notes
10:10PM — Saturday, November 3, 2012. Reading time ~8.116666666666667min.
This post is essentially the result of watching Dan Benjamin’s PeepCode screencast, Meet the Command Line and taking bucket-loads of notes.
A few general things to remember about UNIX:
- UNIX is most often case sensitive.
- UNIX will always override a file without warning.
- Help is sometimes
Kclears the screen.
sudois a command that gives you permissions of a superuser. Allowing you to force something to happen when your user account doesn’t have permissions.
- You need to wrap file and folder names with spaces in “quotes”. I.e.
cd "Misc Documents"
cd directorynamechanges the directory to one a level deeper
- Hitting tab whilst typing a directory name will attempt to auto-complete it
cd ..takes you up a level.
cd ../..will take you back two levels.
cd ../directorynamewill take you back a level and into a folder called directory name.
~represents your home directory. Typing
cd ~anywhere will take you ‘home’ (simply typing
cdalone will also take you home). Typing
cd /directorynamewill take you to a folder within your home directory. I.e.
cd /takes you to the root of the drive.
lslists everything in the current directory.
ls -lgives you a ‘long list’; providing more detail.
-ashows hidden files.
-his an abbreviation for ‘human readable’. As file sizes are listed in bytes, this will show them in B, K, M, G.
- these can be strung together. I.e.
ls -lha. But there isn’t any point in doing
ls -h, as it needs the long list to make the long list human readable.
- you can list the contents of the parent directory with
..represents the parent directory. Just as
cd ..takes you to the parent directory.
ls -Rwill show you all files and folders in every level below. But it looks like shit. With ‘Tree’ installed you can type
treeto see the structure in a more graphical way. See Tree, under Programs for more detail.
Showing the Current Path
pwd shows the current path. It’ll output something like:
Copying, Moving and Renaming Files
cpis the command for copying.
cp -R -v samples my-samplesis a copy command.
cpis short for copy, it’s telling it to make a copy of a folder called
samplesand call it
- I think
-Rmeans recursively, which I think means to make the changes to all child files and folders.
- I think
-vmeans verbose. But I don’t know what that means.
- I think
cp filename.txt newfile.txtwill copy a single file.
cp filename.txt folderwill copy the file to a directory called
mvis the command for moving.
- Moving a file to another file is essentially renaming. I.e.
mv file.txt newname.md.
mv newname.md folderwill move
newname.mdto a directory called
folder. Just like copying.
mv newname.md ~will move
newname.mdto your home directory. You could also use
/pathin place of
~to specify an absolute path.
Showing the Contents of a File
cat filename.txtwill show the contents of filename.txt
Saving and Appending Output to a File
tree > tree.txtwill make a file called tree.txt with the directory structure.
cat file.txt > newfile.txtwill take the contents of file.txt and make a new file called newfile.txt with that contents. However, if that file already exists, its contents will be overridden.
>>will append the contents instead of overwriting it.
cat currentfile.txt >> newfile.txtwill put the contents of currentfile.txt at the end of newfile.txt, if newfile.txt doesn’t exist, it will be created. So unless you specifically want to overwrite a file, it’s probably saver to use
Removing a File or Directory
rm filename1.txt filename2.txtto remove more than one file.
rm -i filename.txtremoves with confirmation.
rmdir directorynameis the command to remove a directory. This only works on empty directories.
rm -R directorynameremoves a directory and contents.
mr -f filename.txtforce removes a file without a confirmation.
Make a Directory
- To make a directory structure you can use
mkdir -p dir1/dir2/dir3/dir4/dir5
Make a File
- You can create a new text file with
cat > newfilename.mdwill give you a new line in Terminal. Type some contents for the file
newfilename.md. To save and exit, start a new line and type
- It’s probably easier to just use a text editor. For example
nanogives you a fresh document and you can save from within Nano.
nano newfilename.mdwill create a file called
newfilename.mdand open it in Nano.
Pipes let you chain commands together. They’re represented by
If the output of one command could be the input to the next you could use a pipe like
command1 | command2.
- To connect to a remote server use
uname -awill show you details of the server.
- To transfer files to a remote server use
put filenameis the command to transfer a file to the server.
get filenameis the command to transfer a file from the server.
- Install Tree via Homebrew with
brew install tree.
- Once installed, typing
treewill give you a listing of all the files in the current directory, but also show you all the children in a more graphical way than
- As with
lsyou can type
tree directorynameto see up or down in the structure.
A typical output might look like this:
. ├── folder1 │ └── folder2 │ └── folder3 │ └── folder4 └── test1 ├── test2 │ ├── test2.md │ └── test3 │ ├── test.md │ ├── test3.md │ └── test4 └── test2.md
But if the structure is deep, like in
~, it quickly gets out of hand.
- Use more with a pipe to show the contents of a file standalone by typing
cat filename.txt | more. More will do this for any command that outputs content. This is only useful for big files, as without it, the whole file will be shown and you’ll be looking at the bottom of the content, not the top.
- You can use a
<to tell something to take the contents of the following. For example
more < filename.txt
Nano Text Editor
cat filename.txt | wc will show you a word count of the file.
cat filename.txt | grep keyword will output only the lines in a file that contain ‘keyword’. This appears to be case sensitive.
Gzip and TAR
gzip filename.txtwill compress a file. It will also remove the original file, unlike in the OS X GUI.
gzip -d filename.txt.gzwill de-compress the g-zipped file. It will remove the g-zipped file and just retain the uncompressed file.
Gzip will only compress one file, not a folder. For that you need TAR (short for Tape Archive).
- To ‘make and archive’ of a directory use
tar -cvf filename.tar directoryname. The
cmeans create, the
vmeans verbose and
fmeans you want to specify a file name.
- By default—unlike Gzip—TAR will leave the original directory.
A TAR file is an archive, but it is not compressed.
gzip filename.tarto get
Using the previous TAR command with the
-z flag, you can Gzip it at the same time.
tar -czvf filename.tgz directoryname(.tgz is short for .tar.gz)
To expand a TAR file, you need to do it from a new folder. Otherwise it’ll dump the contents in the current folder. (It seems like if you compressed a folder, though, that it will expand that folder, rather than it’s contents. So unless there’s a folder with the same name in the current directory, you’re alright.)
tar -xzvf ~/filename.tgzwill expand the archive
filename.tgzfrom the parent folder into the current folder.