Application Deployment and Security · Topic 8 of 8
Bash Commands for Files, Directories and Variables
Use common Bash commands to navigate directories, manage files, view text and set environment variables, with permissions, pipes and redirection.
- 4 min
pwdshows the current directory,lslists it andcdchanges it.mkdir,cp,mv,rmandtouchmanage files and directories.rm -rremoves a directory and its contents, with no undo.
What it is
Bash is the command line shell found on most Linux systems and many developer machines, and it is the usual way to run commands and scripts.
Developers and network automation engineers use it to move around the file system, manage files, set variables and chain tools together.
This page covers the basic commands that appear in exam questions and scripts.
How it works
You give Bash a command, with options and arguments, and it prints the result or acts on files.
Directory navigation
$ pwd
/home/user
$ ls
$ ls -l
$ ls -a
$ cd projects
$ cd ..
$ cd ~
$ cd /etc
| Command | Meaning |
|---|---|
pwd | Print the current directory |
ls | List files. -l gives a long listing and -a shows hidden files |
cd NAME | Change directory |
cd .. | Go up one level |
cd ~ or cd | Go to your home directory |
A path that starts with a slash is absolute, and it begins at the root of the file system. Any other path is relative to the current directory. A single dot means the current directory.
File and directory management
$ mkdir backups
$ mkdir -p app/logs
$ touch notes.txt
$ cp notes.txt backups/
$ mv notes.txt notes_old.txt
$ rm notes_old.txt
$ rm -r backups
| Command | Meaning |
|---|---|
mkdir | Make a directory. -p creates parent directories as needed |
touch | Create an empty file, or update its time stamp |
cp | Copy a file. Use -r for a directory |
mv | Move or rename a file |
rm | Remove a file. Use -r to remove a directory and its contents |
There is no recycle bin. A removed file is gone, so check the name before you press Enter.
Viewing and searching text
$ cat config.yaml
$ head -n 5 app.log
$ tail -n 20 app.log
$ grep ERROR app.log
$ wc -l app.log
cat prints a file. head and tail show the first and last lines. grep prints lines that match a pattern, and wc -l counts lines.
Pipes and redirection
The pipe sends the output of one command into the next. Redirection sends output to a file.
$ grep ERROR app.log | wc -l
$ ls -l > files.txt
$ echo "done" >> files.txt
A single > creates the file or overwrites it. A double >> appends to the end.
Permissions
$ ls -l script.sh
-rw-r--r-- 1 user user 120 Jan 10 09:00 script.sh
$ chmod +x script.sh
$ ./script.sh
The first column shows the type and then three groups of three letters, for the owner, the group and everyone else. The letters are r for read, w for write and x for execute. The chmod +x command adds execute permission, so you can run the file with ./script.sh.
Environment variables
An environment variable is a named value that programs can read. Set one with export, read it with a dollar sign, and list all with env.
$ export API_BASE=sandbox.example
$ echo $API_BASE
sandbox.example
$ env
$ echo $PATH
$ unset API_BASE
A plain assignment such as NAME=value sets a shell variable only. Adding export passes it to programs that you start.
Variables set this way last until the shell closes. To keep them, add the line to a startup file such as .bashrc.
PATH is a list of directories where Bash looks for commands. HOME is your home directory.
Scripts read variables with the same dollar sign, and Python reads them with os.environ. This is the usual way to pass a token to a script without writing it in the code.
$ export API_KEY=abc123
$ python3 script.py
Getting help
man ls opens the manual page for a command, and many tools accept --help.
A leading dot makes a file hidden. Use ls -a to see files such as .bashrc and .gitignore.
What to remember for the exam
Seven facts cover this topic.
pwdshows the current directory,lslists it andcdchanges it.mkdir,cp,mv,rmandtouchmanage files and directories.rm -rremoves a directory and its contents, with no undo.grepsearches text, and a pipe passes output from one command to the next.>overwrites a file and>>appends to it.export NAME=valuesets an environment variable and$NAMEreads it.chmod +xmakes a script executable.
Common mistakes
Four mistakes are typical.
Using > when you meant >>. The first overwrites the file.
Forgetting export. The variable stays in the shell and the program you start cannot see it.
Running a script without execute permission. Add it with chmod +x, or start the file with bash script.sh.
Putting spaces around the equals sign. NAME = value is an error. Write NAME=value.
Be careful with rm -r, especially as an administrator. A wrong path can delete a whole project.
Check yourself
Work through these three questions.
What is the difference between
>and>>?The single
>creates or overwrites a file, and the double>>appends to the end.How do you make an environment variable available to a program that you start?
Set it with
export NAME=valuebefore starting the program.Which command makes a script executable?
chmod +x script.sh.
Topic 8 of 8 in Application Deployment and Security
Test yourself on Application Deployment and Security
The full Cisco CCNA 200-901 DEVASC question bank has 808 questions, with timed practice exams and an explanation for every answer.
This study guide is independent and is not affiliated with, endorsed by, or sponsored by Cisco. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.