Command Line Flashcards
What’s the difference between abc/ and /abc?
The path ‘abc/’ (or ‘./abc’ and ‘./abc/’) are relative to the current working directory.
The path ‘/abc’, however, is the file or directory in the ROOT directory.
How do you access the root directory? Example?
Using the / symbol.
e.g. $ /abc
Access the directory abc in the root directory.
How do you access the current directory? Example?
Using . or ./ symbols
e.g. ‘import from ./components’
(Import a file from the components folder that is located in the same directory as the file with the line of code.)
How do you access a directory one level up? Example?
Using .. or ../ symbols
e.g. $ ../../abc
Access the “grandparent” folder that is two directories up.
What is the home directory and how do you access it?
Using ~ symbol
The home directory is the directory you are placed in when you log in (i.e. when you log in to the mac, you are placed in ~ jennyhai).
What is the wildcard symbol? Example?
Using the * (aka splat or glob) symbol
The wildcard symbol can represent and characters.
e.g. $ cp ../folder/* ./
(Copy all files in ‘folder’ to current directory.)
How do you create a set of nested directories?
Using ‘mkdir -p’
e.g. ‘mkdir -p ~/parents/children/grandchildren’
(Create nested directories in home directory if they do not already exist.)
How do you copy and rename a folder?
Using ‘cp -r’
e.g. $ cp -r grandchildren ../nephews
(Copy the grandchildren folder located in the current folder to the parent folder and also rename it ‘nephews’.)
What is the tree command?
Use the ‘tree’ command to view the directory tree of a certain folder.
e.g. 'tree ~/home' └── parents ├── children │ ├── bob │ └── grandchildren │ └── bob └── nephews └── bob
What is a command? Where are they located?
A command is just a file. ‘ls’, ‘mkdir’, and ‘cd’ are all files. Files that can be used as commands are called executables. These files have special characters in the beginning to tell computer how to execute. They also have scrips or machine language as their content and executable permission.
e.g. $ /bin/echo “Hello World”
(Print “Hello World”)
How do you show the list of hidden files?
$ ls -a
(Show all files including hidden files)
$ ls -d
(Show only hidden files)
What is the fastest way to move multiple files in a folder except for one?
The fastest way till be to move all files in the folder using ‘$ mv * ~/newfolder’ and then move back the single file ‘$ mv ~/newfolder/1.file ./’
How do you find the path of your current location in the command line?
Using ‘pwd’ command
What are environment variables in the command line?
Environment variables are stored data that can be referenced and retrieved at a later point using a name.
e.g. $ MESSAGE=’Hello World’
$ echo $MESSAGE
==> Hello World
NOTE: Method above will only temporarily modify/create environment variables. Once the terminal is closed and restarted, the variables will no longer exist.
How do you change environment variables permanently?
Modify the .bash_profile and .bashrc files to permanently modify environment variables. .bash_profile is a log-in shell that runs every time the terminal starts.
i.e. export PATH=”/path/to/my/executables-directory:$PATH”
(adding the above line of code into .bashrc will modify the PATH variable)