{"id":10811,"date":"2024-12-12T06:46:55","date_gmt":"2024-12-12T06:46:55","guid":{"rendered":"https:\/\/blog.trustedhosting.in\/?p=10811"},"modified":"2024-12-12T06:46:55","modified_gmt":"2024-12-12T06:46:55","slug":"how-to-file-manipulation-in-linux","status":"publish","type":"post","link":"https:\/\/www.webystrata.com\/blog\/how-to-file-manipulation-in-linux\/","title":{"rendered":"How to File Manipulation in Linux"},"content":{"rendered":"<p>&nbsp;<\/p>\n<figure id=\"attachment_10813\" aria-describedby=\"caption-attachment-10813\" style=\"width: 2560px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.trustedhosting.in\/reseller-hosting.html\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-10813 size-full\" title=\"How to File Manipulation in Linux\" src=\"https:\/\/blog.trustedhosting.in\/wp-content\/uploads\/2024\/12\/4798639_49302-1-scaled.jpg\" alt=\"How to File Manipulation in Linux\" width=\"2560\" height=\"2560\" srcset=\"https:\/\/www.webystrata.com\/blog\/wp-content\/uploads\/2024\/12\/4798639_49302-1-scaled.jpg 2560w, https:\/\/www.webystrata.com\/blog\/wp-content\/uploads\/2024\/12\/4798639_49302-1-300x300.jpg 300w, https:\/\/www.webystrata.com\/blog\/wp-content\/uploads\/2024\/12\/4798639_49302-1-1024x1024.jpg 1024w, https:\/\/www.webystrata.com\/blog\/wp-content\/uploads\/2024\/12\/4798639_49302-1-150x150.jpg 150w, https:\/\/www.webystrata.com\/blog\/wp-content\/uploads\/2024\/12\/4798639_49302-1-768x768.jpg 768w, https:\/\/www.webystrata.com\/blog\/wp-content\/uploads\/2024\/12\/4798639_49302-1-1536x1536.jpg 1536w, https:\/\/www.webystrata.com\/blog\/wp-content\/uploads\/2024\/12\/4798639_49302-1-2048x2048.jpg 2048w\" sizes=\"auto, (max-width: 2560px) 100vw, 2560px\" \/><\/a><figcaption id=\"caption-attachment-10813\" class=\"wp-caption-text\">\u00a0<\/figcaption><\/figure>\n<h1>How to File Manipulation in Linux<\/h1>\n<p>This guide is only going to show an overview of the<a href=\"https:\/\/www.trustedhosting.in\/reseller-hosting.html\" target=\"_blank\" rel=\"noopener\"> commands<\/a>. To find out more about each command you can run the following command:<\/p>\n<div class=\"highlight-bash notranslate\">\n<div class=\"highlight\">\n<pre>  man <span class=\"o\">[<\/span>command<span class=\"o\">]<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<p>The following is the test directory structure we\u2019ll be using throughout the upcoming examples:<\/p>\n<div class=\"highlight-console notranslate\">\n<div class=\"highlight\">\n<pre><span class=\"gp\">[root@c7 test]# <\/span>ll\r\n<span class=\"go\">total 4<\/span>\r\n<span class=\"go\">drwxr-xr-x 2 root     root     4096 Aug 11 14:45 directoryone<\/span>\r\n<span class=\"go\">-rw-r--r-- 1 another  root        0 Aug 11 14:41 fileone.txt<\/span>\r\n<span class=\"go\">-rw-r--r-- 1 testuser testuser    0 Aug 11 14:41 filethree.txt<\/span>\r\n<span class=\"go\">-rw-r--r-- 1 root     root        0 Aug 11 14:41 filetwo.txt<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">ll<\/span><\/code>\u00a0is just an alias for\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">ls<\/span>\u00a0<span class=\"pre\">-l<\/span><\/code>.<\/p>\n<section id=\"cp\">\n<h2><code class=\"docutils literal notranslate\"><span class=\"pre\">cp<\/span><\/code><\/h2>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">cp<\/span><\/code>\u00a0is used to copy files and directories.<\/p>\n<div class=\"highlight-console notranslate\">\n<div class=\"highlight\">\n<pre><span class=\"gp\">[root@c7 test]# <\/span>cp fileone.txt copyfileone.txt\r\n<span class=\"gp\">[root@c7 test]# <\/span>ll\r\n<span class=\"go\">total 4<\/span>\r\n<span class=\"go\">-rw-r--r-- 1 root     root        0 Aug 11 14:46 copyfileone.txt<\/span>\r\n<span class=\"go\">drwxr-xr-x 2 root     root     4096 Aug 11 14:45 directoryone<\/span>\r\n<span class=\"go\">-rw-r--r-- 1 another  another     0 Aug 11 14:41 fileone.txt<\/span>\r\n<span class=\"go\">-rw-r--r-- 1 testuser testuser    0 Aug 11 14:41 filethree.txt<\/span>\r\n<span class=\"go\">-rw-r--r-- 1 root     root        0 Aug 11 14:41 filetwo.txt<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<p>As you can see here, the file\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">fileone.txt<\/span><\/code>\u00a0was copied to\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">copyfileone.txt<\/span><\/code>; however the permissions were set to be owned by\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">root<\/span><\/code>\u00a0and part of the group\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">root<\/span><\/code>.<\/p>\n<div class=\"highlight-console notranslate\">\n<div class=\"highlight\">\n<pre><span class=\"gp\">[root@c7 test]# <\/span>whoami\r\n<span class=\"go\">root<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<p>This is because we\u2019re logged in as the\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">root<\/span><\/code>\u00a0user. If you want to maintain the permissions, you need to use the\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">-p<\/span><\/code>\u00a0flag.<\/p>\n<div class=\"highlight-console notranslate\">\n<div class=\"highlight\">\n<pre><span class=\"gp\">[root@c7 test]# <\/span>ll\r\n<span class=\"go\">total 4<\/span>\r\n<span class=\"go\">-rw-r--r-- 1 another  another     0 Aug 11 14:41 copyfileone.txt<\/span>\r\n<span class=\"go\">drwxr-xr-x 2 root     root     4096 Aug 11 14:45 directoryone<\/span>\r\n<span class=\"go\">-rw-r--r-- 1 another  another     0 Aug 11 14:41 fileone.txt<\/span>\r\n<span class=\"go\">-rw-r--r-- 1 testuser testuser    0 Aug 11 14:41 filethree.txt<\/span>\r\n<span class=\"go\">-rw-r--r-- 1 root     root        0 Aug 11 14:41 filetwo.txt<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<p>You can copy directories in the same manner but running just\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">cp<\/span>\u00a0<span class=\"pre\">directoryone<\/span>\u00a0<span class=\"pre\">copydirectoryone<\/span><\/code>\u00a0will just copy the directory itself but none of its contents. To copy the contents as well you need to pass the\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">-R<\/span><\/code>\u00a0flag. The\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">-p<\/span><\/code>\u00a0and\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">-R<\/span><\/code>\u00a0flag can be combined to the\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">-a<\/span><\/code>\u00a0flag.<\/p>\n<div class=\"highlight-console notranslate\">\n<div class=\"highlight\">\n<pre><span class=\"gp\">[root@c7 test]# <\/span>cp -a directoryone copydirectoryone\r\n<span class=\"gp\">[root@c7 test]# <\/span>ll\r\n<span class=\"go\">total 8<\/span>\r\n<span class=\"go\">drwxr-xr-x 2 root     root     4096 Aug 11 14:45 copydirectoryone<\/span>\r\n<span class=\"go\">-rw------- 1 root     root        0 Aug 11 14:54 copyfileone.txt<\/span>\r\n<span class=\"go\">drwxr-xr-x 2 root     root     4096 Aug 11 14:45 directoryone<\/span>\r\n<span class=\"go\">-rw------- 1 another  another     0 Aug 11 14:41 fileone.txt<\/span>\r\n<span class=\"go\">-rw-r--r-- 1 testuser testuser    0 Aug 11 14:41 filethree.txt<\/span>\r\n<span class=\"go\">-rw-r--r-- 1 root     root        0 Aug 11 14:41 filetwo.txt<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<\/section>\n<section id=\"mv\">\n<h2><code class=\"docutils literal notranslate\"><span class=\"pre\">mv<\/span><\/code><\/h2>\n<p>The move command is used to move or rename a file.<\/p>\n<div class=\"highlight-console notranslate\">\n<div class=\"highlight\">\n<pre><span class=\"gp\">[root@c7 test]# <\/span>mv filetwo.txt directoryone\r\n<span class=\"gp\">[root@c7 test]# <\/span>ll directoryone\/\r\n<span class=\"go\">total 0<\/span>\r\n<span class=\"go\">-rw-r--r-- 1 root root 0 Aug 11 14:41 filetwo.txt<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<p>Here\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">filetwo.txt<\/span><\/code>\u00a0has been moved to\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">directoryone<\/span><\/code>.<\/p>\n<div class=\"highlight-console notranslate\">\n<div class=\"highlight\">\n<pre><span class=\"gp\">[root@c7 test]# <\/span>mv copyfileone.txt renamedcopy.txt\r\n<span class=\"gp\">[root@c7 test]# <\/span>ll\r\n<span class=\"go\">total 8<\/span>\r\n<span class=\"go\">drwxr-xr-x 2 root     root     4096 Aug 11 14:45 copydirectoryone<\/span>\r\n<span class=\"go\">drwxr-xr-x 2 root     root     4096 Aug 11 14:58 directoryone<\/span>\r\n<span class=\"go\">-rw------- 1 another  another     0 Aug 11 14:41 fileone.txt<\/span>\r\n<span class=\"go\">-rw-r--r-- 1 testuser testuser    0 Aug 11 14:41 filethree.txt<\/span>\r\n<span class=\"go\">-rw------- 1 root     root        0 Aug 11 14:54 renamedcopy.txt<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<p>Here the file\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">copyfileone.txt<\/span><\/code>\u00a0has been renamed to\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">renamedcopy.txt<\/span><\/code><\/p>\n<\/section>\n<section id=\"rm\">\n<h2><code class=\"docutils literal notranslate\"><span class=\"pre\">rm<\/span><\/code><\/h2>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">rm<\/span><\/code>\u00a0is used to delete files and directories.<\/p>\n<div class=\"highlight-console notranslate\">\n<div class=\"highlight\">\n<pre><span class=\"gp\">[root@c7 test]# <\/span>rm renamedcopy.txt\r\n<span class=\"go\">rm: remove regular empty file 'renamedcopy.txt'? y<\/span>\r\n<span class=\"gp\">[root@c7 test]# <\/span>ll\r\n<span class=\"go\">total 8<\/span>\r\n<span class=\"go\">drwxr-xr-x 2 root     root     4096 Aug 11 14:45 copydirectoryone<\/span>\r\n<span class=\"go\">drwxr-xr-x 2 root     root     4096 Aug 11 14:58 directoryone<\/span>\r\n<span class=\"go\">-rw------- 1 another  another     0 Aug 11 14:41 fileone.txt<\/span>\r\n<span class=\"go\">-rw-r--r-- 1 testuser testuser    0 Aug 11 14:41 filethree.txt<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<p>Here the file\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">renamedcopy.txt<\/span><\/code>\u00a0is deleted. As you can see, there is a prompt for this. This can be overridden with the\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">-f<\/span><\/code>\u00a0flag.<\/p>\n<div class=\"highlight-console notranslate\">\n<div class=\"highlight\">\n<pre><span class=\"gp\">[root@c7 test]# <\/span>rm -f renamedcopy.txt\r\n<span class=\"gp\">[root@c7 test]# <\/span>ll\r\n<span class=\"go\">total 8<\/span>\r\n<span class=\"go\">drwxr-xr-x 2 root     root     4096 Aug 11 14:45 copydirectoryone<\/span>\r\n<span class=\"go\">drwxr-xr-x 2 root     root     4096 Aug 11 14:58 directoryone<\/span>\r\n<span class=\"go\">-rw------- 1 another  another     0 Aug 11 14:41 fileone.txt<\/span>\r\n<span class=\"go\">-rw-r--r-- 1 testuser testuser    0 Aug 11 14:41 filethree.txt<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<p>To delete a directory you need to pass the\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">-R<\/span><\/code>\u00a0flag.<\/p>\n<div class=\"highlight-console notranslate\">\n<div class=\"highlight\">\n<pre><span class=\"gp\">[root@c7 test]# <\/span>rm -R directoryone\r\n<span class=\"go\">rm: descend into directory 'directoryone'? y<\/span>\r\n<span class=\"go\">rm: remove regular empty file 'directoryone\/filetwo.txt'? y<\/span>\r\n<span class=\"go\">rm: remove directory 'directoryone'? y<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<p>Again, this prompts you. You can override this by using\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">-Rf<\/span><\/code>.<\/p>\n<div class=\"highlight-console notranslate\">\n<div class=\"highlight\">\n<pre><span class=\"gp\">[root@c7 test]# <\/span>rm -Rf directoryone\/\r\n<span class=\"gp\">[root@c7 test]# <\/span>ll\r\n<span class=\"go\">total 4<\/span>\r\n<span class=\"go\">drwxr-xr-x 2 root     root     4096 Aug 11 14:45 copydirectoryone<\/span>\r\n<span class=\"go\">-rw------- 1 another  another     0 Aug 11 14:41 fileone.txt<\/span>\r\n<span class=\"go\">-rw-r--r-- 1 testuser testuser    0 Aug 11 14:41 filethree.txt<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<p>It is possible to add the\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">*<\/span><\/code>\u00a0on the end of the\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">rm<\/span>\u00a0<span class=\"pre\">-Rf<\/span><\/code>\u00a0command; however, we would advise you never to do this as if this command is run in the \/ folder on your server, this will cause all the contents of the server to be deleted. Instead, if you want to delete a directory, you should specify the directory.<\/p>\n<div class=\"highlight-console notranslate\">\n<div class=\"highlight\">\n<pre><span class=\"gp\">[root@c7 ~]# <\/span>rm -Rf \/root\/test<\/pre>\n<\/div>\n<\/div>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; How to File Manipulation in Linux This guide is only going to show an overview of the commands. To&hellip;<\/p>\n","protected":false},"author":1,"featured_media":10813,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-10811","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.webystrata.com\/blog\/wp-json\/wp\/v2\/posts\/10811","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webystrata.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webystrata.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webystrata.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webystrata.com\/blog\/wp-json\/wp\/v2\/comments?post=10811"}],"version-history":[{"count":3,"href":"https:\/\/www.webystrata.com\/blog\/wp-json\/wp\/v2\/posts\/10811\/revisions"}],"predecessor-version":[{"id":10816,"href":"https:\/\/www.webystrata.com\/blog\/wp-json\/wp\/v2\/posts\/10811\/revisions\/10816"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webystrata.com\/blog\/wp-json\/wp\/v2\/media\/10813"}],"wp:attachment":[{"href":"https:\/\/www.webystrata.com\/blog\/wp-json\/wp\/v2\/media?parent=10811"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webystrata.com\/blog\/wp-json\/wp\/v2\/categories?post=10811"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webystrata.com\/blog\/wp-json\/wp\/v2\/tags?post=10811"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}