2: Working with Files
- Page ID
- 433278
\( \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}\)Creating Files
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.
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
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
more
displays content of a file one page at a time
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
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:
Example:
echo "123 \n456 \n789" > new_file_txt
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)
- Output:
-
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
tail
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
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
- Output:
-
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
- 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
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.
- Output:
-
to be replaced
To sort the content of the file, we use the command:
- sort
sort file_to_sort.txt
- Output:
-
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
- 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
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)
zip
sudo zip -r soundfiles samples/
- tree: shows tree in directories
tree
- uniq
filters out duplicate lines in a test file
wc: word count
wc -l line count