08-28-2018, 04:22 PM
Linux for Beginners: Moving Things Around
![[Image: linux-for-beginners-moving-things-around.jpg]](http://www.sickgaming.net/blog/wp-content/uploads/2018/08/linux-for-beginners-moving-things-around.jpg)
![[Image: linux-for-beginners-moving-things-around.jpg]](http://www.sickgaming.net/blog/wp-content/uploads/2018/08/linux-for-beginners-moving-things-around.jpg)
In previous installments of this series, you learned about directories and how permissions to access directories work. Most of what you learned in those articles can be applied to files, except how to make a file executable.
So let’s deal with that before moving on.
No .exe Needed
In other operating systems, the nature of a file is often determined by its extension. If a file has a .jpg extension, the OS guesses it is an image; if it ends in .wav, it is an audio file; and if it has an .exe tacked onto the end of the file name, it is a program you can execute.
This leads to serious problems, like trojans posing as documents. Fortunately, that is not how things work in Linux. Sure, you may see occasional executable file endings in .sh that indicate they are runnable shell scripts, but this is mostly for the benefit of humans eyeballing files, the same way when you use, the names of executable files show up in bright green.
The fact is most applications have no extension at all. What determines whether a file is really program is the x (for executable) bit. You can make any file executable by running
regardless of its extension or lack thereof. The in the command above sets the x bit and the says you are setting it for all users. You could also set it only for the group of users that own the file (), or for only one user, the owner ().
Although we will be covering creating and running scripts from the command line later in this series, know that you can run a program by writing the path to it and then tacking on the name of the program on the end:
Or, if you are currently in the same directory, you can use:
There are other ways of making your program available from anywhere in the directory tree (hint: look up the environment variable), but you will be reading about those when we talk about shell scripting.
Copying, Moving, Linking
Obviously, there are more ways of modifying and handling files from the command line than just playing around with their permissions. Most applications will create a new file if you still try to open a file that doesn’t exist. Both
and
(nano and vim being to popular command line text editors) will create an empty test.txt file for you to edit if test.txt didn’t exist beforehand.
You can also create an empty file by touching it:
Will create a file, but not open it in any application.
You can use to make a copy of a file in another location or under a new name:
You can also copy a whole bunch of files:
The instruction above copies all the PNG files in the current directory into an images/ directory hanging off of your home directory. The images/ directory has to exist before you try this, or will show an error. Also, be warned that, if you copy a file to a directory that contains another file with the same name, will silently overwrite the old file with the new one.
You can use
If you want to warn you of any dangers (the options stands for interactive).
You can also copy whole directories, but you need the option for that:
The option stands for recursive, meaning that will drill down into directory_a, copying over all the files and subdirectories contained within. I personally like to include the option, as it makes verbose, meaning that it will show you what it is doing instead of just copying silently and then exiting.
The command moves stuff. That is, it changes files from one location to another. In its simplest form, looks a lot like :
The command above makes new_test.txt appear and test.txt disappear.
Moves all the PNG files in the current directory to a directory called images/ hanging of your home directory. Again you have to be careful you do not overwrite existing files by accident. Use
the same way you would with if you want to be on the safe side.
Apart from moving versus copying, another difference between and is when you move a directory:
No need for a recursive flag here. This is because what you are really doing is renaming the directory, the same way in the first example, you were renaming the file*. In fact, even when you “move” a file from one directory to another, as long as both directories are on the same storage device and partition, you are renaming the file.
You can do an experiment to prove it. is a tool that lets you measure how long a command takes to execute. Look for a hefty file, something that weighs several hundred MBs or even some GBs (say, something like a long video) and try copying it from one directory to another like this:
In bold is what you have to type into the terminal and below what outputs. The number to focus on is the one on the first line, real time. It takes nearly 4 seconds to copy the 355 MBs of hefty_file.mkv to another_directory/.
Now let’s try moving it:
Moving is nearly instantaneous! This is counterintuitive, since it would seem that would have to copy the file and then delete the original. That is two things has to do versus ‘s one. But, somehow, is 1000 times faster.
That is because the file system’s structure, with all its tree of directories, only exists for the users convenience. At the beginning of each partition there is something called a partition table that tells the operating system where to find each file on the actual physical disk. On the disk, data is not split up into directories or even files. There are tracks, sectors and clusters instead. When you “move” a file within the same partition, what the operating system does is just change the entry for that file in the partition table, but it still points to the same cluster of information on the disk.
Yes! Moving is a lie! At least within the same partition that is. If you try and move a file to a different partition or a different device, is still fast, but is noticeably slower than moving stuff around within the same partition. That is because this time there is actually copying and erasing of data going on.
Renaming
There are several distinct command line utilities around. None are fixtures like or and they can work in slightly different ways. What they all have in common is that they are used to change parts of the names of files.
In Debian and Ubuntu, the default utility uses regular expressions (patterns of strings of characters) to mass change files in a directory. The instruction:
will change all the extensions of files with the extension JPEG to jpg. The file IMG001.JPEG becomes IMG001.jpg, my_pic.JPEG becomes my_pic.jpg, and so on.
Another version of available by default in Manjaro, a derivative of Arch, is much simpler, but arguably less powerful:
This does the same renaming as you saw above. In this version, is the string of characters you want to change, is what you want to change it to, and represents all the files in the current directory.
The bottom line is that you are better off using if all you want to do is rename one file or directory, and that’s because is realiably the same in all distributions everywhere.
Learning more
Check out the both and ‘s man pages to learn more. Run
or
to read about all the options these commands come with and which make them more powerful and safer to use.
![[Image: linux-for-beginners-moving-things-around.jpg]](http://www.sickgaming.net/blog/wp-content/uploads/2018/08/linux-for-beginners-moving-things-around.jpg)
![[Image: linux-for-beginners-moving-things-around.jpg]](http://www.sickgaming.net/blog/wp-content/uploads/2018/08/linux-for-beginners-moving-things-around.jpg)
In previous installments of this series, you learned about directories and how permissions to access directories work. Most of what you learned in those articles can be applied to files, except how to make a file executable.
So let’s deal with that before moving on.
No .exe Needed
In other operating systems, the nature of a file is often determined by its extension. If a file has a .jpg extension, the OS guesses it is an image; if it ends in .wav, it is an audio file; and if it has an .exe tacked onto the end of the file name, it is a program you can execute.
This leads to serious problems, like trojans posing as documents. Fortunately, that is not how things work in Linux. Sure, you may see occasional executable file endings in .sh that indicate they are runnable shell scripts, but this is mostly for the benefit of humans eyeballing files, the same way when you use
Code:
ls --colorThe fact is most applications have no extension at all. What determines whether a file is really program is the x (for executable) bit. You can make any file executable by running
Code:
chmod a+x some_programregardless of its extension or lack thereof. The
Code:
xCode:
aCode:
g+xCode:
u+xAlthough we will be covering creating and running scripts from the command line later in this series, know that you can run a program by writing the path to it and then tacking on the name of the program on the end:
Code:
path/to/directory/some_programOr, if you are currently in the same directory, you can use:
Code:
./some_programThere are other ways of making your program available from anywhere in the directory tree (hint: look up the
Code:
$PATHCopying, Moving, Linking
Obviously, there are more ways of modifying and handling files from the command line than just playing around with their permissions. Most applications will create a new file if you still try to open a file that doesn’t exist. Both
Code:
nano test.txtand
Code:
vim test.txt(nano and vim being to popular command line text editors) will create an empty test.txt file for you to edit if test.txt didn’t exist beforehand.
You can also create an empty file by touching it:
Code:
touch test.txtWill create a file, but not open it in any application.
You can use
Code:
cpCode:
cp test.txt copy_of_test.txtYou can also copy a whole bunch of files:
Code:
cp *.png /home//imagesThe instruction above copies all the PNG files in the current directory into an images/ directory hanging off of your home directory. The images/ directory has to exist before you try this, or
Code:
cpCode:
cpYou can use
Code:
cp -i *.png /home//imagesIf you want
Code:
cpCode:
-iYou can also copy whole directories, but you need the
Code:
-rCode:
cp -rv directory_a/ directory_bThe
Code:
-rCode:
cpCode:
-vCode:
cpThe
Code:
mvCode:
mvCode:
cpCode:
mv test.txt new_test.txtThe command above makes new_test.txt appear and test.txt disappear.
Code:
mv *.png /home//imagesMoves all the PNG files in the current directory to a directory called images/ hanging of your home directory. Again you have to be careful you do not overwrite existing files by accident. Use
Code:
mv -i *.png /home//imagesthe same way you would with
Code:
cpApart from moving versus copying, another difference between
Code:
mvCode:
cpCode:
mv directory_a/ directory_bNo need for a recursive flag here. This is because what you are really doing is renaming the directory, the same way in the first example, you were renaming the file*. In fact, even when you “move” a file from one directory to another, as long as both directories are on the same storage device and partition, you are renaming the file.
You can do an experiment to prove it.
Code:
timeCode:
$ time cp hefty_file.mkv another_directory/
real 0m3,868s user 0m0,016s sys 0m0,887sIn bold is what you have to type into the terminal and below what
Code:
timeNow let’s try moving it:
Code:
$ time mv hefty_file.mkv another_directory/
real 0m0,004s
user 0m0,000s sys 0m0,003sMoving is nearly instantaneous! This is counterintuitive, since it would seem that
Code:
mvCode:
mvCode:
cpCode:
mvThat is because the file system’s structure, with all its tree of directories, only exists for the users convenience. At the beginning of each partition there is something called a partition table that tells the operating system where to find each file on the actual physical disk. On the disk, data is not split up into directories or even files. There are tracks, sectors and clusters instead. When you “move” a file within the same partition, what the operating system does is just change the entry for that file in the partition table, but it still points to the same cluster of information on the disk.
Yes! Moving is a lie! At least within the same partition that is. If you try and move a file to a different partition or a different device,
Code:
mvRenaming
There are several distinct command line
Code:
renameCode:
cpCode:
mvIn Debian and Ubuntu, the default
Code:
renameCode:
rename 's/\.JPEG$/.jpg/' *will change all the extensions of files with the extension JPEG to jpg. The file IMG001.JPEG becomes IMG001.jpg, my_pic.JPEG becomes my_pic.jpg, and so on.
Another version of
Code:
renameCode:
rename .JPEG .jpg *This does the same renaming as you saw above. In this version,
Code:
.JPEGCode:
.jpgCode:
*The bottom line is that you are better off using
Code:
mvCode:
mvLearning more
Check out the both
Code:
mvCode:
cpCode:
man cpor
Code:
man mvto read about all the options these commands come with and which make them more powerful and safer to use.

