Files and Directories in Practical DevSecOps Labs: Creating, Editing, and Navigating
Working with files and directories is one of the most common tasks in the Practical DevSecOps labs. This article covers essential commands for creating, navigating, editing, renaming, and removing files and directories.
Table of Contents
Navigating Directories
Creating and listing directories
mkdir new-directory # creates a new directory (silent on success)
ls new-directory # lists contents (empty produces no output)
Checking your current location
pwd
/
/ means you are in the root directory of the filesystem.
Changing directories
cd new-directory # enter a directory
cd .. # go to parent directory
cd . # stay in current directory (rarely needed)
Removing directories
rmdir new-directory # removes an empty directory
Note:
rmdironly removes empty directories. Userm -rto remove directories with contents (use with caution).
Working with Files
Creating and editing files with nano
nano myfile
Once in the editor, type your content, then:
- Save:
Ctrl + O, thenEnter - Exit:
Ctrl + X(orCtrl + X, thenYto confirm save)
Viewing file contents with cat
cat myfile
cat reads a file's content and displays it on the terminal.
Renaming files with mv
mv myfile newfile
mv (move) is used to rename or move files.
Deleting files with rm
rm newfile
Warning:
rmpermanently deletes the file. There is no trash bin in Linux.
Here Documents
You can create files non-interactively using here documents (useful for automation and CI/CD):
cat > filename <<EOL
Some text content
Some text content 2
Some text content 3
EOL
How it works:
catfollowed by>creates a file<<EOLstarts a here document block — everything between the markers is treated as input- The closing
EOLon its own line ends the block
Verify with
lsto see the file, andcat filenameto review its contents.
Common Questions
| Question | Answer |
|---|---|
| How do I go back to the previous directory? | Use cd .. to go to the parent directory. |
What's the difference between . and ..? |
. refers to the current directory, .. refers to the parent directory. |
| Can I create a file without an editor? | Yes, use touch filename to create an empty file, or a here document (cat > filename <<EOL). |
Why does rmdir fail on some directories? |
rmdir only works on empty directories. Remove contents first, or use rm -r directory. |
| How do I view hidden files? | Use ls -la to show all files, including those starting with a dot (.). |
Wrap-Up
Mastering file and directory operations is essential for every lab exercise. Remember the core commands: mkdir, cd, pwd, ls, cat, nano, mv, rm, and rmdir. Use here documents for quick, non-interactive file creation. Happy file management!