Skip to main content
Chemistry LibreTexts

6: File Commands

  • Page ID
    469636
  • \( \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}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    File Commands

    (transclusion link)

    cat

    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
    

    echo

    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 

    echo can create a new file when used with a redirect and place its content in the file

    echo "insert this text into new file" > newfile.txt
    

    find

    find a file in a directory

    note the "i" in iname makes it case insensitve

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

    grep

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

    • grep   string_content   file_name :  Global Regular Expression Print 

     

    head

    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)
    

    less 

     

    more

    displays content of a file one page at a time 

    mv

    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

    rm

     

    • rm    file_name :  remove
      rm test_file_2.txt
      

    sed

    sed- Stream editor

    sed [intial word/new word] filename

    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 's/course/class/' test_file_3.txt
    

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

    cat test_file_3.txt
    
    Output:

    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

     

    sort

    sort file_to_sort.txt

    sort switches

    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
    
    Output:

    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
    Output:

    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

    Output:

    to be added 

     

    tail

    Shows end of file

     

    touch

    • 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.

    a

     

    wc

    word count

    wc switches

     

    wc -l line count


    6: File Commands is shared under a not declared license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?