ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Linux: Working with Files

    IT Discussion
    linux system admin sam linux administration
    5
    9
    3.0k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • scottalanmillerS
      scottalanmiller
      last edited by scottalanmiller

      One of the most important and common tasks that we will do on any system is working with files. No different in Linux. So learning the basic file and directory manipulation commands will be useful.

      In this lesson we are going to work with:

      • cp: The copy command, no different from copy in the Windows Command Shell.
      • mv: The move command, it handles moving (as opposed to copying) a file as well as renaming.
      • mkdir: The MaKe DIRectory command, more a less straightforward, it allows you to create a new directory.
      • touch: Touching a file is a handy way to make a new file or update the last time that it has been accessed.
      • rm: Remove (delete) a file.
      • rmdir: Remove (delete) a directory.
      • file: Test a file to determine what type of file it is.

      And from our last lesson:

      • ls: The "list" command, which is used to show the contents of a directory. Similar to the Windows DIR command.

      Files in Linux work as expected if you are coming from other operating systems. We will just do a few simple commands to demonstrate usage.

      First we will cd into the /tmp directory. This is a great place to experiment and test files because it is easy to clean everything up when we are done.

      # cd /tmp
      # ls
      

      Our ls command should show us a mostly empty directly, but likely we will have a few temporary system files such as:

      ks-script-IXEDel  yum.log
      

      We will now use touch to create a file of our own:

      # touch myfile
      

      UNIX Silent Success: You will notice that after you run this command there is no feedback other than the command prompt returning and acting ready for your next command. In the UNIX world, it is standard for silence to signify success and output to only be needed on an error condition (or, of course, when the command is designed to output like the ls command.) So do not be surprised that we do not see our new file appearing until we run another command to look for it. We will need to run ls again to see our new file.

      # ls
      

      Is your new file there? Hopefully it is visible. Using ls is showing us the full contents of the /tmp directory, which is our current working directory (you can always verify this with pwd or by looking at your command prompt.) Using a list command is common, but not always practical especially if we have a lot of files in our directory.

      Another way to test if our file was created is to test the file directly using the file command. The file command is very handy, not just for testing if a file exists but also to get a general idea of what kind of file it is.

      # file myfile 
      myfile: empty
      

      Remember about absolute and relative paths. We used cd to move into /tmp and used relatives paths in our examples. We could have used absolute paths as well, like file /tmp/myfile instead of file myfile.

      Of course the file command shows our new file as being empty, that is exactly what it is. The touch command creates an empty file as we have provided no information about what to put into the file. It's as empty as a file can be.

      Now that we know how to create empty files, we should use mkdir to make a new, empty directory, too. We will call our new directory mydirectory.

      # mkdir mydirectory
      

      If we list the contents of our current working directory, /tmp, again using ls we should see that our new directory is there now.

      # ls
      

      If your new directory is there, we can now take the file that we created earlier and move it into the new directory. The mv command takes two arguments, first the file we are moving and second the location to which we are moving it.

      # mv myfile mydirectory/
      

      Now if we do an ls again we will see that our file is no longer there. But if we use ls mydirectory to look at the contents of our new directory, we should see that our file has moved from one location to the other.

      # ls
      # ls mydirectory
      

      Something that is very important to understand about the mv command is how it does what it does. The mv command actually changes the location metadata or "pointer" of our file and does not move the file on disk. This means that the mv command can move a file of any size anywhere on a single filesystem without using any additional space and happens, effectively, instantly. It requires only the tiniest disk access and the size or type of file is not a dependency. If we use the mv command to move between filesytems (more on filesystems coming up in later lessons) the the filesystem metadata cannot be updated and the file must actually be copied between the systems resulting in a very different operation.

      Now that we know how to move files, we will learn how to copy them. The cp command uses the same arguments as the mv command. We will use something new in this command, too, a special symbol to designate the current working directory. This symbol is a period: ".".

      # cp mydirectory/myfile .
      

      Using the "." designation makes it quick and easy to tell Linux where we want to put the file when we are already where we want the file to go. You will use this designation often for many purposes. We could have used any of the following commands as well to have done the same thing:

      # cp /tmp/mydirectory/myfile .
      # cp mydirectory/myfile /tmp/
      # cp /tmp/mydirectory/myfile /tmp/
      

      Or we could have moved our location and handled it that way:

      # cd mydirectory
      # cp myfile /tmp/
      

      It should go without saying, but I will point it out for completeness, that the cp command actually copies the file in the traditional sense. It makes a second copy of the file on disk. The two files are not related to each other once the copy operation has completed. Even if we are copying on a single filesystem the copy command is going to double the space used on disk. If the original file is 1MB, both the original and the copy will use 1MB each. So using the cp command might take some time and may introduce a fair amount of disk activity if the files are large. The cp could even cause a disk to fill up if the files are large.

      Now, assuming that our current working directory is still /tmp (if it is not, just use cd /tmp to return to it) we can use ls to see that our file is copied here. Notice that the name remains identical, it does not get a special name like copy_of_myfile or anything like that.

      Since we have a copy of our file in our current working directory, let's try renaming it using the mv command.

      # mv myfile copy_of_myfile
      # ls
      

      The ls command should show us our file renamed, in place.

      That is it, we can now create, copy, move, rename and test files and directories. Now we will just delete the files and directory that we have created to clean up after ourselves and our lesson will be over. The rm command is very easy, it simply needs to know what file to remove.

      # rm copy_of_myfile
      

      Here is a surprise, the rm command does not simply remove the file but instead asks us if we really want to do this. This is a safety precaution. To continue, enter "y" for yes and hit enter. This safety feature in the rm command is not universal and is a modification to the standard rm command made by Red Hat to make their Linux distro a little safer. This modification is done using an alias which we will look at in depth in a future lesson. But it is very important to know that on some systems the rm command will not present this additional check and may delete your file silently assuming that you knew what you were doing.

      We have deleted our file. We cannot simply use the rm command to remove the directory that we made:

      # rm mydirectory
      

      Notice that we get an error saying that this directory cannot be removed because it is a directory. This is why we have the rmdir command. Try that instead.

      # rmdir mydirectory
      

      Another error message. We cannot use rmdir to delete a directory that is not empty. Another important safety feature. Without this we might accidentally think that we are deleting a directory and accidentally delete a large number of contents! There is a way to to this that we will learn later. For now, we will empty the contents of the directory manually.

      # rm mydirectory/myfile
      # rmdir mydirectory
      

      All cleaned up.

      1 Reply Last reply Reply Quote 5
      • JaredBuschJ
        JaredBusch
        last edited by

        I would include ls in your bullet list as it is not the same command as Windows people are used to unlike cd.

        scottalanmillerS 1 Reply Last reply Reply Quote 0
        • scottalanmillerS
          scottalanmiller @JaredBusch
          last edited by

          @JaredBusch said:

          I would include ls in your bullet list as it is not the same command as Windows people are used to unlike cd.

          Added.

          1 Reply Last reply Reply Quote 0
          • DashrenderD
            Dashrender
            last edited by

            It's funny for several years now, I've been using ls on Windows, and of course it errors and I have to remember to use dir.

            scottalanmillerS 1 Reply Last reply Reply Quote 1
            • scottalanmillerS
              scottalanmiller @Dashrender
              last edited by

              @Dashrender said:

              It's funny for several years now, I've been using ls on Windows, and of course it errors and I have to remember to use dir.

              I know a lot of people make aliases so that they can use ls to hit DIR.

              1 Reply Last reply Reply Quote 1
              • brianlittlejohnB
                brianlittlejohn
                last edited by

                If you are in powershell it doesn't matter.

                1 Reply Last reply Reply Quote 1
                • MogrithM
                  Mogrith
                  last edited by

                  2 typos
                  between filessytems (more on filesystems
                  does net get a special name like copy_of_myfile
                  net-> not?

                  scottalanmillerS 1 Reply Last reply Reply Quote 0
                  • scottalanmillerS
                    scottalanmiller @Mogrith
                    last edited by

                    @Mogrith said in Linux: Working with Files:

                    2 typos
                    between filessytems (more on filesystems
                    does net get a special name like copy_of_myfile
                    net-> not?

                    Thanks... and welcome to the community!

                    1 Reply Last reply Reply Quote 0
                    • scottalanmillerS
                      scottalanmiller
                      last edited by

                      Fixed

                      1 Reply Last reply Reply Quote 0
                      • 1 / 1
                      • First post
                        Last post