{"id":924,"date":"2021-03-11T12:10:08","date_gmt":"2021-03-11T11:10:08","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=924"},"modified":"2021-03-15T09:30:53","modified_gmt":"2021-03-15T08:30:53","slug":"bash-manage-folder-file","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2021\/03\/11\/bash-manage-folder-file\/","title":{"rendered":"Bash &#8211; Manage folder &#038; File"},"content":{"rendered":"\n<h4>I &#8211; How to create a repertory in Linux from terminal<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir -p &lt;folder name><\/code><\/pre>\n\n\n\n<p><em><code>mkdir -p<\/code>&nbsp;would not give  an error if the directory already exists and the contents for the directory will not change.<\/em><\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4>II &#8211; How to create a file&nbsp;<\/h4>\n\n\n\n<p class=\"has-text-align-left\">Create an empty json file named foo.json:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>touch foo.json\n\n\/\/or\n\ncat > foo.json<\/code><\/pre>\n\n\n\n<p><em>Add data and press&nbsp;<span class=\"has-inline-color has-vivid-cyan-blue-color\"><kbd>CTRL<\/kbd>+<kbd>D<\/kbd><\/span>&nbsp;to save the foo.json when using cat on Linux<\/em><\/p>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-red-color\">How to create a text file using the CAT command.<\/span><\/h5>\n\n\n\n<p>To create a text file named sales.txt, type the following command and then press&nbsp;<kbd>[Enter]<\/kbd>&nbsp;key:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat > sales.txt<\/code><\/pre>\n\n\n\n<p>Now type your lines of text. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Month, Profit\n01\/Apr\/2004, $1500\n01\/May\/2004, $1600\n01\/Jun\/2004, $1450<\/code><\/pre>\n\n\n\n<p>When done and you need to save and exit, press&nbsp;<kbd>Ctrl<\/kbd>+<kbd>D<\/kbd>&nbsp;to return to the bash shell prompt.<\/p>\n\n\n\n<p><span class=\"has-inline-color has-black-color\"> To view file use <strong>cat<\/strong> or <strong>more<\/strong>\/<strong>less<\/strong> command<\/span><span class=\"has-inline-color has-vivid-cyan-blue-color\">:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat sales.txt\n\n\/\/Or\nmore sales.txt<\/code><\/pre>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-red-color\">Creating a file in Linux using the echo or printf<\/span><\/h5>\n\n\n\n<p>Let us create a file called quote1.txt<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo \"While I thought that I was learning how to live, I have been learning how to die.\" > quote1.txt\n\n\/\/or \n\nprintf 'Study nature, love nature, stay close to nature. It will never fail you.\\n' > quote2.txt<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><em>ps: <span class=\"has-inline-color has-vivid-cyan-blue-color\">You can also redirect the output of a command to a file<\/span>:<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ cat file > copy_file<\/code><\/pre>\n\n\n\n<p>..<span class=\"has-inline-color has-vivid-cyan-blue-color\"><em>.or append to it<\/em><\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ cat file >> copy_file<\/code><\/pre>\n\n\n\n<p>Use the the&nbsp;<kbd><span class=\"has-inline-color has-vivid-red-color\">&gt;&gt;<\/span><\/kbd>&nbsp;instead of&nbsp;<kbd><span class=\"has-inline-color has-vivid-red-color\">&gt;<\/span><\/kbd>&nbsp;to append data to existing file and to avoid overwriting files.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><span class=\"has-inline-color has-vivid-red-color\"><strong>Ps: If you need root permission:<\/strong><\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> sudo sh -c 'cat file > copy_file'<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4>III &#8211; How to Remove (Delete) Directory&nbsp;<\/h4>\n\n\n\n<p> <em>Be extra careful when removing files or directories from the command line because once the directory is deleted using the commands, it cannot be fully recovered.<\/em><\/p>\n\n\n\n<p><em>On most Linux filesystems, deleting a directory requires write permission on the directory and its content. Otherwise, you will get \u201cOperation not permitted\u201d error.<\/em><\/p>\n\n\n\n<p>Directory names with a space in them must be escaped with a backslash (<code>\/<\/code>).<\/p>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-red-color\">Removing Directories with&nbsp;<\/span><code><strong><span class=\"has-inline-color has-vivid-purple-color\">rmdir<\/span><\/strong><\/code><span class=\"has-inline-color has-vivid-red-color\">&nbsp;<a href=\"https:\/\/linuxize.com\/post\/remove-directory-linux\/#removing-directories-with-rmdir\">#<\/a><\/span><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>rmdir dir1<\/code><\/pre>\n\n\n\n<p>If the directory is not empty, you will get the following error:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rmdir: failed to remove 'dir1': No such file or directory<\/code><\/pre>\n\n\n\n<p>In this case, you will need to use the&nbsp;<code><span class=\"has-inline-color has-vivid-purple-color\">rm<\/span><\/code>&nbsp;command or manually remove the directory contents before you can delete it.<\/p>\n\n\n\n<h5 id=\"removing-directories-with-rm\"><span class=\"has-inline-color has-vivid-red-color\">Removing Directories with&nbsp;<\/span><code><strong><sup><span class=\"has-inline-color has-vivid-purple-color\">rm<\/span><\/sup><\/strong><\/code><span class=\"has-inline-color has-vivid-red-color\">&nbsp;<a href=\"https:\/\/linuxize.com\/post\/remove-directory-linux\/#removing-directories-with-rm\">#<\/a><\/span><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>rm -r dir1<\/code><\/pre>\n\n\n\n<p>If a directory or a file within the directory is write-protected, you will be prompted to confirm the deletion. To remove a directory without being prompted, use the&nbsp;<code>-f<\/code>&nbsp;option:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rm -rf dir1<\/code><\/pre>\n\n\n\n<p>To remove multiple directories at once, invoke the&nbsp;<code>rm<\/code>&nbsp;command, followed by the names of the directories separated by space. The command below will remove each listed directory and their contents:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rm -r dir1 dir2 dir3<\/code><\/pre>\n\n\n\n<h4>To Search in a File<\/h4>\n\n\n\n<p>To print any line from a file that contains a specific pattern of characters, in our case\u00a0<code><strong>thumb<\/strong><\/code>\u00a0in the file\u00a0<code><strong>syslog<\/strong><\/code>, run the command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>grep thumb syslog<\/code><\/pre>\n\n\n\n<p>Grep will display every line where there is a match for the word\u00a0<code><strong>thumb<\/strong><\/code>. When executing this command, you do not get exact matches. Instead, the terminal prints the lines with words containing the string of characters you entered. Here is an example:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/03\/image-1024x203.png\" alt=\"\" class=\"wp-image-936\" width=\"861\" height=\"170\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/03\/image-1024x203.png 1024w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/03\/image-300x59.png 300w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/03\/image-768x152.png 768w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/03\/image.png 1503w\" sizes=\"(max-width: 861px) 100vw, 861px\" \/><\/figure>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-cyan-blue-color\">To Search in Multiple Files<\/span><\/h5>\n\n\n\n<p>To search multiple files with the grep command, insert the filenames you want to search, separated with a space character.<\/p>\n\n\n\n<p>In our case, the grep command to match the word\u00a0<code><strong>phoenix<\/strong><\/code>\u00a0in three files\u00a0<strong><code>sample<\/code>,<code>sample2<\/code>, and\u00a0<code><strong>sample3\u00a0<\/strong><\/code>looks like this example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>grep phoenix sample sample2 sample3<\/code><\/pre>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-cyan-blue-color\">Search All Files in Directory<\/span><\/h5>\n\n\n\n<p>To search all files in the current directory, use an asterisk instead of a filename at the end of a grep command.<\/p>\n\n\n\n<p>In this example, we use<strong>\u00a0<em>nix\u00a0<\/em><\/strong>as a search criterion:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>grep nix *<\/code><\/pre>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-cyan-blue-color\">To Find Whole Words Only<\/span><\/h5>\n\n\n\n<p>Grep allows you to find and print the results for whole words only. To search for the word\u00a0<strong>phoenix<\/strong>\u00a0in all files in the current directory, append\u00a0<strong><em>\u2013w\u00a0<\/em><\/strong>to the grep command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>grep -w phoenix *<\/code><\/pre>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-cyan-blue-color\">To List Names of Matching Files<\/span><\/h5>\n\n\n\n<p>Sometimes, you only need to see the names of the files that contain a word or string of characters and exclude the actual lines. To print only the filenames that match your search, use the\u00a0<strong>-l\u00a0<\/strong>operator:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>grep -l phoenix *<\/code><\/pre>\n\n\n\n<p>The output shows the exact filenames that contain\u00a0<code><strong>phoenix<\/strong><\/code>\u00a0in the current directory but does not print the lines with the corresponding word:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/phoenixnap.com\/kb\/wp-content\/uploads\/2019\/03\/Use-grep-to-find-files-excluding-the-actual-lines.png\" alt=\"example how to find files that contain a word or string of characters excluding the actual lines\"\/><\/figure>\n\n\n\n<p>ps: <em>\u00a0use the recursive search operator\u00a0<strong><code>-r<\/code><\/strong>\u00a0to include all subdirectories in your search.<\/em><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I &#8211; How to create a repertory in Linux from terminal mkdir -p&nbsp;would not give an error if the directory already exists and the contents for the directory will not change. II &#8211; How to create a file&nbsp; Create an empty json file named foo.json: Add data and press&nbsp;CTRL+D&nbsp;to save the foo.json when using cat [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[169],"tags":[172,236,95,237,173,235],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/924"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=924"}],"version-history":[{"count":7,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/924\/revisions"}],"predecessor-version":[{"id":937,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/924\/revisions\/937"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=924"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=924"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=924"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}