Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Linux for Beginners: Moving Things Around

#1
Linux for Beginners: Moving Things Around

<div style="margin: 5px 5% 10px 5%;"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/08/linux-for-beginners-moving-things-around.jpg" width="1500" height="678" title="" alt="" /></div><div><div><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/08/linux-for-beginners-moving-things-around.jpg" class="ff-og-image-inserted" /></div>
<p>In previous installments of this series, <a href="https://www.linux.com/blog/learn/2018/5/manipulating-directories-linux">you learned about directories</a> and how <a href="https://www.linux.com/blog/learn/intro-to-linux/2018/7/users-groups-and-other-linux-beasts-part-2">permissions to access directories work</a>. Most of what you learned in those articles can be applied to files, except how to make a file executable.</p>
<p>So let’s deal with that before moving on.</p>
<h3>No <i>.exe</i> Needed</h3>
<p>In other operating systems, the nature of a file is often determined by its extension. If a file has a <i>.jpg</i> extension, the OS guesses it is an image; if it ends in <i>.wav</i>, it is an audio file; and if it has an <i>.exe</i> tacked onto the end of the file name, it is a program you can execute.</p>
<p>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 <i>.sh</i> 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 --color</code>, the names of executable files show up in bright green.</p>
<p>The fact is most applications have no extension at all. What determines whether a file is really program is the <i>x</i> (for <i>executable</i>) bit. You can make any file executable by running</p>
<pre>
chmod a+x some_program
</pre>
<p>regardless of its extension or lack thereof. The <code>x</code> in the command above sets the <i>x</i> bit and the <code>a</code> says you are setting it for <i>all</i> users. You could also set it only for the group of users that own the file (<code>g+x</code>), or for only one user, the owner (<code>u+x</code>).</p>
<p>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:</p>
<pre>
path/to/directory/some_program
</pre>
<p>Or, if you are currently in the same directory, you can use:</p>
<pre>
./some_program
</pre>
<p>There are other ways of making your program available from anywhere in the directory tree (hint: look up the <code>$PATH</code> environment variable), but you will be reading about those when we talk about shell scripting.</p>
<h3>Copying, Moving, Linking</h3>
<p>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</p>
<pre>
nano test.txt
</pre>
<p>and</p>
<pre>
vim test.txt
</pre>
<p>(<a href="https://www.nano-editor.org/">nano</a> and <a href="https://www.vim.org/">vim</a> being to popular command line text editors) will create an empty <i>test.txt</i> file for you to edit if <i>test.txt</i> didn’t exist beforehand.</p>
<p>You can also create an empty file by <i>touching</i> it:</p>
<pre>
touch test.txt
</pre>
<p>Will create a file, but not open it in any application.</p>
<p>You can use <code>cp</code> to make a copy of a file in another location or under a new name:</p>
<pre>
cp test.txt copy_of_test.txt
</pre>
<p>You can also copy a whole bunch of files:</p>
<pre>
cp *.png /home//images
</pre>
<p>The instruction above copies all the PNG files in the current directory into an <i>images/</i> directory hanging off of your home directory. The <i>images/</i> directory has to exist before you try this, or <code>cp</code> will show an error. Also, be warned that, if you copy a file to a directory that contains another file with the same name, <code>cp</code> will silently overwrite the old file with the new one.</p>
<p>You can use</p>
<pre>
cp -i *.png /home//images
</pre>
<p>If you want <code>cp</code> to warn you of any dangers (the <code>-i</code> options stands for <i>interactive</i>).</p>
<p>You can also copy whole directories, but you need the <code>-r</code> option for that:</p>
<pre>
cp -rv directory_a/ directory_b
</pre>
<p>The <code>-r</code> option stands for <i>recursive</i>, meaning that <code>cp</code> will drill down into <i>directory_a</i>, copying over all the files and subdirectories contained within. I personally like to include the <code>-v</code> option, as it makes <code>cp</code> <i>verbose</i>, meaning that it will show you what it is doing instead of just copying silently and then exiting.</p>
<p>The <code>mv</code> command moves stuff. That is, it changes files from one location to another. In its simplest form, <code>mv</code> looks a lot like <code>cp</code>:</p>
<pre>
mv test.txt new_test.txt
</pre>
<p>The command above makes <i>new_test.txt</i> appear and <i>test.txt</i> disappear.</p>
<pre>
mv *.png /home//images
</pre>
<p>Moves all the PNG files in the current directory to a directory called <i>images/</i> hanging of your home directory. Again you have to be careful you do not overwrite existing files by accident. Use</p>
<pre>
mv -i *.png /home//images
</pre>
<p>the same way you would with <code>cp</code> if you want to be on the safe side.</p>
<p>Apart from moving versus copying, another difference between <code>mv</code> and <code>cp</code>is when you move a directory:</p>
<pre>
mv directory_a/ directory_b
</pre>
<p>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<a href="https://www.linux.com/blog/2018/8/linux-beginners-moving-things-around#rename">*</a>. 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.</p>
<p>You can do an experiment to prove it. <code>time</code> 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:</p>
<pre>
$ <b>time cp hefty_file.mkv another_directory/</b>
real 0m3,868s user 0m0,016s sys 0m0,887s
</pre>
<p>In bold is what you have to type into the terminal and below what <code>time</code> outputs. The number to focus on is the one on the first line, <i>real</i> time. It takes nearly 4 seconds to copy the 355 MBs of <i>hefty_file.mkv </i> to <i>another_directory/</i>.</p>
<p>Now let’s try moving it:</p>
<pre>
$ <b>time mv hefty_file.mkv another_directory/</b>
real 0m0,004s
user 0m0,000s sys 0m0,003s
</pre>
<p>Moving is nearly instantaneous! This is counterintuitive, since it would seem that <code>mv</code> would have to copy the file and then delete the original. That is two things <code>mv</code> has to do versus <code>cp</code>‘s one. But, somehow, <code>mv</code> is 1000 times faster.</p>
<p>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 <i>partition table</i> 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. <a href="https://en.wikipedia.org/wiki/Disk_sector">There are tracks, sectors and clusters instead</a>. 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.</p>
<p>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>mv</code> 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.</p>
<h3>Renaming</h3>
<p>There are several distinct command line <code>rename</code> utilities around. None are fixtures like <code>cp</code> or <code>mv</code> and they can work in slightly different ways. What they all have in common is that they are used to change <i>parts</i> of the names of files.</p>
<p>In Debian and Ubuntu, the default <code>rename</code> utility uses <a href="https://en.wikipedia.org/wiki/Regular_expression">regular expressions</a> (patterns of strings of characters) to mass change files in a directory. The instruction:</p>
<pre>
rename 's/\.JPEG$/.jpg/' *
</pre>
<p>will change all the extensions of files with the extension <i>JPEG</i> to <i>jpg</i>. The file <i>IMG001.JPEG</i> becomes <i>IMG001.jpg</i>, <i>my_pic.JPEG</i> becomes <i>my_pic.jpg</i>, and so on.</p>
<p>Another version of <code>rename</code> available by default in Manjaro, a derivative of Arch, is much simpler, but arguably less powerful:</p>
<pre>
rename .JPEG .jpg *
</pre>
<p>This does the same renaming as you saw above. In this version, <code>.JPEG</code> is the string of characters you want to change, <code>.jpg</code> is what you want to change it to, and <code>*</code> represents all the files in the current directory.</p>
<p>The bottom line is that you are better off using <code>mv</code> if all you want to do is rename one file or directory, and that’s because <code>mv</code> is realiably the same in all distributions everywhere.</p>
<h3>Learning more</h3>
<p>Check out the both <code>mv</code> and <code>cp</code>‘s <i>man</i> pages to learn more. Run</p>
<pre>
man cp
</pre>
<p>or</p>
<pre>
man mv
</pre>
<p>to read about all the options these commands come with and which make them more powerful and safer to use.</p>
</div>
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016