File Manipulation in Linux Shell Scripting

In this lesson, we are going to see file manipulation in the Linux system which includes how to read and write files. In the end, we will see commonly used file-related commands.

Write to a file in Linux

In the first example, we are going to create a file outputText.txt using the touch command and then going to write “Hello World” to it. For writing to the file, we can just use output redirection using “>>“. Let’s see the example.

Save the following code as writer_basic.sh in a directory in your Linux machine:

#!/usr/bin/env bash

# create a file using touch command
touch outputText.txt

# write hello world to outputText.txt
echo "Hello World" >> outputText.txt

Run the writer_basic.sh using bash command:

$ bash writer_basic.sh

It will create outputText.txt file in the same directory with “Hello World” in it.

Read From a file in Linux

In this example, we are going to read from a file names.txt and print the content of each line in the console. Save the following code in a file named reader.sh :

#!/usr/bin/env bash
COUNT=1
#Internal Field Separator (IFS) that is used for word splitting 
#after expansion and to split lines into words with the read builtin command
while IFS='' read -r LINE
do
  echo "LINE $COUNT: $LINE"
  ((COUNT++))
#$1 gets the file passed a parameter while running the script
done < "$1"

exit 0

In the same directory, create the names.txt file with the following content:

Abel
Maria
Richard
Tom
Rene

Run the reader.sh file like:

$ bash reader.sh names.txt

Output will be like:

run file reader script
run reader.sh file

In the final example, we are going to see how to read and write to and from a file. For doing that, we will read from the “names.txt” file, create a file “output.txt” using the touch command, and write the contents available in the names.txt file to it.

Save the following code as writer.sh :

#!/usr/bin/env bash
COUNT=1
# create a file
touch output.txt

#Internal Field Separator (IFS) that is used for word splitting 
#after expansion and to split lines into words with the read builtin command
while IFS='' read -r LINE
do
  echo "LINE $COUNT: $LINE" >> output.txt
  ((COUNT++))
#$1 gets the file passed a parameter while running the script
done < "$1"

exit 0

Run the writer.sh file as like below:

$ bash writer.sh names.txt

After reading the names.txt file, it will write to the output.txt with the content of the names.txt file in the same directory.

Commonly Used File Manipulation Commands in Linux System

  1. Create a file in Linux:

a. Create file using touch command:

$ touch info.yaml

Create multiple files using touch command:

$ touch info.yaml data.txt mail.tmp

b. Create file using Redirection operator:

$ > info.yaml

c. Create file with echo command

$ echo "Hello World" > info.yaml
# to create an empty file
$ echo > map.yaml

d. Create file using cat command:

# create the infoTemp.yaml file and redirect the content of the info.yaml to it
$ cat info.yaml > infoTemp.yaml

e. Create file using Heredoc: Heredoc allow us to redirect multiple lines to a file

$ cat << EOF > info.yaml

This is the first line
This is the second line

This is the third line

EOF

2. Copy a file: cp command

Syntax: cp <path/to/the/file> <path/to/the/copy/file>

Example:

$ cp /tmp/32064386.jpg /home/user1/

3. Move a file (Cut/paste): mv command

Syntax: mv <path/to/the/file> <path/to/the/move/file>

Example:

$ mv /tmp/32064386.jpg /home/user1/

4. Rename a file: mv command is used to rename a file.

Example:

$ mv oldFileName.txt newFileName.txt

5. Remove/Delete a file: rm command

Syntax: rm fileName or rm path/to/the/file/fileName

Example:

rm data.txt

See also how to find a file in Linux: Find file in Linux . See complete examples in our GitHub repository.

Follow us on social media

Author