Other Parts of This Series:


DevOps Linux File Manipulation Commands (Photo Credit: Unsplash)

DevOps Linux File Manipulation Commands (Photo Credit: Unsplash)

Story:

Rasel grabbed the knowledge and understanding about the Linux kernel, how it boots, and the Linux file system. As the server runs mostly using CLI and no UI like Windows, so Rasel now wanted to learn the commands that help to work with files and directories. Like commands that are used for file and directory creation and content in its manipulation.


Basic Structure of a Command:

The general structure of a Linux command line looks like:

1
command [-flag(s)] [-option(s) [value]] [argument(s)]
  • command - is an executable file or program to be run/execute. Like: cd, ls, cat.
  • flag - A short switch (usually with -) that modifies the command’s behavior. Like: -l.
  • option - Like a flag, but often with a value or longer form. Like: –sort=time.
  • argument - The target on which the command operates. Like: a file or directory name.

Helper Commands:

Linux has some helper commands which helps to find and understand other command, in case need clarifications or clues. Some of the most common of them are:

1
[command] --help

This gives description and prototype of a command.

1
man [command]

This gives manual of a command.

1
whatis [command]

This gives single line description of a command.

1
apropos [keyword]

This gives possible command with the given keyword.

Example:

1
2
3
4
cp --help 
man cp
whatis ls
apropos cd        

Linux has some basic commands which helps to create, rename, delete and move directories. Means command that help to work with directory. Some of them are:

1
mkdir [directory name]

This create a new directory with the given name.

1
cd [directory name]

This checkout into the given directory name.

1
pwd

This print or give current working directory.

1
rmdir [directory name]

This remove or delete the directory with given name.

1
mv [source directory] [destination directory]

This move source directory to the given destination directory. Interestingly this command is also used for rename as well.

Example:

1
2
3
4
5
mkdir newFolder
cd newFolder
pwd
mv newFolder oldFolder
rmdir oldFolder        

Linux has commands which helps to create, rename, delete and move files. Means command that help to work with file. Some of them are:

1
file [file name]

This command gives the file type of the given file.

1
touch [file name]

This command create a new file with the given name.

1
rm [file name]

This command remove or delete the file with given name.

1
mv [source file path] [destination file path]

This command move the file source file to the destination. This also used for rename as well.

Example:

1
2
3
4
touch a.txt
file a.txt
mv a.txt b.txt
rm b.txt        

File and Directory Structure Explore and Visit Commands:

Linux has commands which helps to explore the file and directory structures. Also help to locate and jump from one to another location. Some of them are:

1
tree

This command gives the tree of the file and directory relationship.

1
ls

This command gives the file and directory list.

1
cd [directory]

This command checkout into the given directory name.

Example:

1
2
3
tree
ls
cd folder

Linux has commands which helps to work with file content. Means manipulating a file. Some of them are:

1
cat [file name]

This command view the full content of file with the given name.

1
head [file name]

This command view the first 10 lines content of file with the given name.

1
tail [file name]

This command view the last 10 lines content of file with the given name.

1
cat > [file name]

This command start inserting for content to file with the given name.

1
cp [source file path] [destination file path]

This command copy the file source file to the destination.

1
find . -name "*.log"

This command find files with the given regex.

1
grep "*error" [file name]

This command find content with the given regex within given file name.

1
sed 's/before/after/g' [file name]

This command replace content before with after of given file name.

Other command as well like, sort for sorting content, uniq for getting unique content, cut for getting split column content etc.

Example:

1
2
3
4
5
6
cat a.txt
cat > a.txt
head a.txt
tail a.txt
sed 's/before/after/g' a.txt
grep "*error" a.txt        

Working With Vim:

Vim is an advanced text editor based on the older vi. It’s keyboard-driven, super fast, and works inside the terminal, perfect for server environments.

Vim has 3 main mode:

  1. Normal Mode (Default): Navigate and issue commands (Esc key)
  2. Insert Mode: Insert text (like typing normally) (Press i, I, a etc.)
  3. Command Mode: Save, quit, search, etc. (Press : from Normal)
1
vim [file name]

This command open the file for working with the given name.

Vim Summary:

  • Open file - vim filename
  • Insert mode - i, a, o, O
  • Save + Quit - :wq or ZZ
  • Quit without saving - :q!
  • Search - /pattern then n / N
  • Replace all - :%s/old/new/g
  • Copy/Cut/Paste - yy, dd, p
  • Undo / Redo - u, Ctrl + r
  • Go to line - :line_number or G / gg

Summary:

In Linux, working with files and directories is an essential skill for DevOps. You can create files using touch and directories using mkdir. To view or organize them, you use commands like ls, cp to copy, mv to move or rename, and rm to delete. To view file content, you use cat, less, head, and tail, while tools like vim help you edit files directly from the terminal. Searching inside files can be done with grep, and locating files can be done with find. For text manipulation, you use commands like cut, sed, sort, uniq to extract, modify, or organize text data.