Skip to main content
Chemistry LibreTexts

2: Working with Files

  • Page ID
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    Creating Files


    • used to create any type of file with zero size
    touch demo.txt

     The creation of files can be done in different ways, one of them is using redirection.

    ls >

    Let's take the output of our previous ls command and save it in a text file. 

    This can be done by using ">" after the command and adding the name of the file to write to, as follows:

    ls > ls_output.txt

    With this command, the output of the ls command is redirected to the text file.


    Viewing files


    We can view the content of the file on the terminal by using the command:

    The following command allows you to see what your operating system is

    • cat file _name:  Displays content of a file (concatenate)
      cat ls_output.txt

    cat does not support keyboard scrolling

     cat /etc/os-release


    displays content of a file one page at a time 


    We can display some text that we type into the command line, not necessarily reading it from a file, by using the command:

    • echo "text":  printing text on the terminal
    echo "display new text"

    The echo command prints its parameters 


    We can also create text files using the echo command, by redirecting the text in the parameters, as follows:

    echo "display new text 1" > test_file_1
    echo "display new text 2" > test_file_2
    We can check the test files that we created again using the ls command. The output would be:




    To use echo and create a file with multiple-line content, use \n when a new line is needed:


    echo "123 \n456 \n789" > new_file_txt



    To display the beginning of a file, we use the command:

    • head   file_name :  Displays the beginning (head) of a file
    head -n1 ls_output.txt       (displays first 1 line of ls_output.txt)
    head -n2 ls_output.txt       (displays first 2 lines of ls_output.txt)



    If we want to scroll up/down arrows to look at the file content, we use the command:

    • less 
    less /tmp/test/ls_output.txt


    To display the end of a file, we use the command:

    • tail   file_name :
      tail /tmp/test/ls_output.txt
    • To display a certain number of lines (for example 20 lines) we can add the option -n20 as follows:
    tail -n20 /tmp/test/ls_output.txt   
    • To display new lines in real-time, we can add the option -f as follows:
    tail -f /tmp/test/ls_output.txt          


    Operations on files


    find a file

    find /home/path -iname filename.extension
    find /home/path -filename *.extension

    mv - moving files

    Let's move one of the test files that we created previously to one of our subdirectories. This is done b the command:

    • mv    file_name    directory_name : move a file to a directory
    mv test_file_1.txt subdir1
    We can check that the file moved to the desired directory by using the ls command. The output would be:



    grep - finding content in a file

    To find a string in a file, we use the command:

    • grep   string_content   file_name :  Global Regular Expression Print 



    Modifying file content

    If we want to modify the content of a file on the terminal, we can use nano through the command:

    • nano   file_name : 
      nano /tmp/test/test_file_2.txt


    Using the nano editor, we use CTRL+O+Enter and CTRL+X to save changes


    Deleting a file

    • rm    file_name :  remove
      rm test_file_2.txt
      rm -rf /tmp/test/subdir1


    To delete a folder, use -rf (recursive force)


    Substituting content of a file

    For command testing purposes, let's create a new file with the following text:

    echo "Course number 1, course number 2, course number 3, course number 4"  > test_file_3.txt

    To find and replace something in a file, we can use the command: 

    • sed  'options'  input_file_name
    sed 's/course/class/' test_file_3.txt

    If we now check the content of the file using cat:

    cat test_file_3.txt

    Every word "course" is replaced by the word "class":

    "Class number 1, class number 2, class number 3, class number 4"


    screenshot to be added


    Sorting file content

    Let's create a new file entitled file_to_sort.txt with the following example content:

    echo "Amanda \nMelody \nRosa \nJohn \nCaleb \nRick" > file_to_sort.txt
    To check the content of the file, we use the "cat" command.


    to be replaced

    To sort the content of the file, we use the command: 

    • sort
    sort file_to_sort.txt


    to be replaced

    By using the sort command, the result is displayed only but not saved. To capture the result we need to store it. For this purpose, the -o option is used.

    To save the sorted content in the same file:

    sort -o file_to_sort.txt file_to_sort.txt 

    To save the sorted content in a separate file different from the original one:

    sort -o new_file_to_sort.txt file_to_sort.txt


    Sorting for Column Number

    Sorting can be done with file content that has more than one column. Let's create a new text file entitled "file_to_sort_columns.txt" in which there are names and ages (2 columns).

    Let's organize them in ascending order. For that, we use the keyword –k in the command and –n for numerical sorting (Since there are two columns, 2 is used with -n)

    sort –k 2n file_to_sort_columns.txt

    to be added 


    Checking the Sorting of the file

    If we want to check if the file was sorted, we can display the file content using the cat command:

    cat file_to_sort_columns.txt

    to be added 


    Removing duplicates

    If there are repeated words in the file, we can use the sort command with -u option to remove the duplicate, as follows:

    sort -u file_to_sort_columns.txt
    Now, we can see that the data is sorted and the repeated items are removed from the output.

    to be added 


     Sorting in Reverse Order

    We can sort the file in reverse order (descending order by default) sort by using -R option with the sort command.

    sort -R file_to_sort_columns.txt


    to be added 


    Other useful commands

    wc – line count, char count, word count, max line length

    unzip – unzip a zip file

    gunzip – unzip a gz file

    bunzip – unzip a bz file

    tar – extract files from a tar archive, (tar file)


    sudo zip -r soundfiles samples/






    • tree:  shows tree in directories
    • uniq

    filters out duplicate lines in a test file


    wc:  word count

    wc -l line count


    This page titled 2: Working with Files is shared under a not declared license and was authored, remixed, and/or curated by Robert Belford.

    • Was this article helpful?