UNIX Command Line Usage Notes
11:10AM — 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
help,-hor--help Command+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"
Commands
Change Directory
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. Typingcd ~anywhere will take you ‘home’ (simply typingcdalone will also take you home). Typingcd /directorynamewill take you to a folder within your home directory. I.e.cd ~/dropbox/notescd /takes you to the root of the drive.
Directory Listing
lslists everything in the current directory.ls -lgives you a ‘long list’; providing more detail.- adding
-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 doingls -h, as it needs the long list to make the long list human readable. - you can list the contents of the parent directory with
ls ..as..represents the parent directory. Just ascd ..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 typetreeto 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:
/Users/andytlr/dropbox/websites
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 calledsamplesand call itmy-samples.- 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 calledfolder. -
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 movenewname.mdto a directory calledfolder. Just like copying. -
mv newname.md ~will movenewname.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.- Using
>>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 filename.txtrm 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
mkdir directoryname- To make a directory structure you can use
-p. I.e.mkdir -p dir1/dir2/dir3/dir4/dir5
Make a File
- You can create a new text file with
cat.cat > newfilename.mdwill give you a new line in Terminal. Type some contents for the filenewfilename.md. To save and exit, start a new line and typeControl+D. - 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 callednewfilename.mdand open it in Nano.
Pipes
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.
Programs
SSH
- To connect to a remote server use
ssh username@server uname -awill show you details of the server.- Type
exitto disconnect.
SFTP
- To transfer files to a remote server use
sftp username@server1 put filenameis the command to transfer a file to the server.get filenameis the command to transfer a file from the server.- Type
exitto disconnect.
Tree
- 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 thanls -R. - As with
lsyou can typetree ..ortree 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 / or ~, it quickly gets out of hand.
More
- 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 examplemore < filename.txt
Nano Text Editor
nano filename.txt
Word Count
cat filename.txt | wc will show you a word count of the file.
Grep
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. Thecmeans create, thevmeans verbose andfmeans 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 getfilename.tar.gz
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 archivefilename.tgzfrom the parent folder into the current folder.
-
It seemed more complicated, but in the last 5min of Dan’s video, there was a section on Secure Copy (SCP), which is an alternative to SFTP.
↩
Have a comment? @me on Twitter or App.net