Just like in Finder (macOS), Explorer (Windows), or Nautilus (Ubuntu), you can navigate and manipulate files and folders in your filesystem using the command line. Each session in your terminal starts in a particular directory and you can traverse the directory structure starting from there. You can use pwd
(print working directory) to find the current working directory.
pwd
This command gives the absolute path to your current working directory.
An absolute path specifies the complete path to a file or folder, ignoring your current working directory. For example, if you were to give someone “absolute” directions to your house, you would start by telling them to be on earth, then go to your continent, then go to your country, then go to your region, etc. The root of the filesystem is referred to as /
.
A relative path specifies the path to a file or folder, taking into account your current working directory. For example, if you were to give someone “relative” directions to your house, you would give them directions from their current location (the relative path from where they are to where you are).
A few directories have special symbols:
/
is the root of the filesystem.
current directory..
parent directory~
shortcut to the home directory (for me, the absolute path would be /Users/mehtad
)Below is an example directory structure from macOS.
/
├── Applications
└── Users
└── mehtad
├── Desktop
├── Development
├── Documents
├── Downloads
├── Movies
├── Music
└── Pictures
In the example above, if I was in the Downloads folder and I wanted the relative path to the Documents folder, that would be ../Documents
.
In the same example above, the absolute path to the Documents folder would be /Users/mehtad/Documents
.
If you are in the Downloads folder, what folder is:
../
../../
../../mehtad/
../../mehtad/./
../../mehtad/./Downloads/../Documents
Notice that those were relative paths, the following are absolute paths that both refer to the same location:
/Users/mehtad/Downloads
~/Downloads
pwd
prints working directory (the directory you are currently in)
cd
To navigate to different directories within your filesystem, you can use the cd
(change directory) command. cd
takes one positional argument, the absolute or relative path to the destination directory.
cd <target_directory>
cd ..
will move you “up” one directory (to the parent directory) and cd ~
will move you back to the “home” directory.
ls
In order to list the contents of the current directory you can use ls
.
ls -a
lists all files, including hidden filesls -l
lists the files in a long format with extra information (permissions, size, last modified date, etc.)ls *
also lists the contents of subdirectories (one level deep) in your working directoryls <path>
lists files in a specific directory (without changing your working directory)tree
If you don’t have tree
installed, on macOS run brew install tree
and on ubuntu run sudo apt-get install tree
.
tree
shows the files and folders in the current working directorytree -L 2
recursively shows the files and directories 2 level deep from the current directorycat
You can view the contents of an individual file (or files) by using cat
. cat
will print the entire content of a file to the screen.
cat <filename>
cat <filename1> <filename2> ...
head
When a file is very long, you can use head
to print he beginning of a file.
head <filename>
prints the head (the first 10 lines) of the filehead -n20 <filename>
prints the first 20 lines of the filetail
In the case of long, constantly changing files, like a log file, you can use tail
.
tail <filename>
prints the tail (the last 10 lines) of the fileless
Last, when reading long files page by page (like when using man
), you can use less
to paginate the contents.
less <filename>
allows you to page or scroll through the fileq
to exit.subl
If you don’t have Sublime Text installed, run brew cask install sublime-text
on macOS and on ubuntu follow these instructions.
subl <folder>
opens the folder in sublimesubl <filepath>
opens the file in sublimeopen
(mac only)open <folder>
opens the Finder in the folder specifiedopen <filepath>
opens a url or file in the default mac programwc
wc <path>
wordcountwc -l <path>
only counts lineswc -w <path>
only counts words. A “word” is defined as any set of characters delimited by a space.wc -c <path>
only counts characters~/Desktop
and run both ls -a
and tree
~/Downloads
and run the ls -l
command to see information about every item in that folderhint! - are you lost? don’t know what to type next?
pwd
to see “where you are”ls
to see what files and folders are therecd <path>
, where The command line not only lets you navigate the file system but also manipulate it by creating new files (touch
), creating directories (mkdir
), deleting files and directories (rm -i
, rm -ir
), copying files and directories (cp -v
, cp -vR
), move/rename files and directories (mv
), and more…
touch
touch <filename>
creates an empty file called <filename>
mkdir
mkdir <dirname>
makes a new directory called <dirname>
rm -i
rm -i <filename>
removes files in interactive mode, in which you are prompted to confirm that you really want to delete the file. It’s best to always use rm -i
rm -ir <dirname>
removes a directory and recursively deletes all of its contentscp -v
cp -v <filename> <new_path>
copies a file from its current location to <newpath>
, leaving the original file unchanged. The -v
is for verbose modecp -vR <dirname> <new_path>
copies a directory recursively to <newpath>
mv
mv <filename> <new path>
moves a file from its current location to <new path>
mv <filename> <new filename>
renames a file without changing its locationComplete the following assignment: https://classroom.github.com/a/d4WK4RbX
find
find <path> -name <name>
will recursively search the specified path (and its subdirectories) and find files and directories with a given <name>
.
for the <path>
to refer to the working directory.<name>
, you can search for an exact match, or use wildcard characters to search for a partial match:
*
specifies any number of any characters, such as find . -name *.py
or find . -name *data*.*
?
specifies one character, such as find . -name ??_*.*
grep
grep <pattern> <filename>
searches a file for a regular expression pattern and prints the matching lines
-i
option to ignore case.grep -r <pattern> <path>
does a recursive search of the path (checks subdirectories) for matches within files
.
for the <path>
to refer to the working directory.grep <pattern>
does a global search (of your entire computer) for matches
Ctrl + c
if you want to cancel the search.find
all items in the universe
folder with the .txt
extension.
cd ~/Development/universe
find . -name "*.txt"
find
all folders in the universe
folder. use man find
to figure out how to get folders only.
hint:
The manual for the find command (man find
) is super long. The answer is in there, but it might take a while to find. You might want to try google instead.