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
prints working directory (the directory you are currently in)
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.
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)
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 directory
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.
- usage:
cat <filename>
- usage:
cat <filename1> <filename2> ...
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 file- This is useful for previewing the contents of a large file without opening it.
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 file
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 file- Hit the spacebar to go down a page, use the arrow keys to scroll up and down, and hit
q
to exit.
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 sublime
open <folder>
opens the Finder in the folder specifiedopen <filepath>
opens a url or file in the default mac program
- count the number of lines and characters in a file
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
- Take some time to cd around and explore your filesystem. See what is at the root, see if you can find some of the files you use daily.
- Navigate to
~/Desktop
and run bothls -a
andtree
- Navigate to
~/Downloads
and run thels -l
command to see information about every item in that folder
hint! - are you lost? don't know what to type next?
- first type
pwd
to see "where you are" - then type
ls
to see what files and folders are there - then think about your next move, it might be
cd <path>
, where is either an absolute or relative filepath for where you want to navigate to.
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 <filename>
creates an empty file called<filename>
- This is useful for creating empty files to be edited at a later time.
mkdir <dirname>
makes a new directory called<dirname>
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 userm -i
rm -ir <dirname>
removes a directory and recursively deletes all of its contents
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 <filename> <new path>
moves a file from its current location to<new path>
mv <filename> <new filename>
renames a file without changing its location
In your home directory, make a folder called Development
. This is where we will keep all of the code for the class.
-
In the
Development
folder, create a new folder calleduniverse
and create the following structure of files and folders.. └── solar_system ├── planets │ ├── asteroid_belt.txt │ ├── earth │ │ └── continents │ │ ├── africa.txt │ │ ├── antarctica.txt │ │ ├── asia.txt │ │ ├── australia.txt │ │ ├── europe.txt │ │ ├── north_america │ │ │ └── countries │ │ │ ├── canada.txt │ │ │ ├── mexico.txt │ │ │ └── united_states.txt │ │ └── south_america.txt │ ├── jupiter.txt │ ├── mars.txt │ ├── mercury.txt │ ├── neptune.txt │ ├── pluto.txt │ ├── saturn.txt │ ├── uranus.txt │ └── venus.txt └── stars └── sun.txt 7 directories, 19 files
-
Delete asteroid belt, since that isn't a planet.
-
Move pluto to a folder called "other".
find <path> -name <name>
will recursively search the specified path (and its subdirectories) and find files and directories with a given<name>
- Use
.
for the<path>
to refer to the working directory.
- Use
- For the
<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 asfind . -name *.py
orfind . -name *data*.*
?
specifies one character, such asfind . -name ??_*.*
grep <pattern> <filename>
searches a file for a regular expression pattern and prints the matching lines- The pattern should be in quotation marks to allow for multiple words.
- The pattern is case-sensitive by default, but you can use the
-i
option to ignore case. - You can use wildcards in the filename to search multiple files, but it only searches the working directory (not subdirectories).
grep -r <pattern> <path>
does a recursive search of the path (checks subdirectories) for matches within files- Use
.
for the<path>
to refer to the working directory.
- Use
grep <pattern>
does a global search (of your entire computer) for matches- Hit
Ctrl + c
if you want to cancel the search.
- Hit
- Much more complex string-matching patterns can be used (which we will cover in a future class).
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.