Please enable JavaScript eh!

 ⌘ Web Mechanic ⌘ 

Bash Scripting


cp | mv

Two more commands dealing with files should finish up giving you a fundamental set of file and directory tools.

cp

You want to copy a file from one place to another, maybe as a backup.

Copying a file is very straight-forward when you know 3 things:

• name of the file (complete with extension)
• name of the source directory
• name of the target directory

If you know all 3 here is an example of the usage:

cp source/filename target/newfilename

To make this simple, cd to a directory containing some files, such as your Desktop.

If you are NOT in the 'Desktop' directory, go there now: 'cd ~/Desktop'.

The file we created there (FileOne) should still be there as well as some directories.

In Terminal enter

cp FileOne "The Second"

We did not need to enter the source directory name because we are already in that location.

The same applies to the target directory name, but remember we need to wrap that target directory name in double-quotes because it contains a space character.

To ensure you've done this right, check to see if the file is there ...

2024-11-22 13:02:53
[~/Desktop] trudge: ls -al "The Second"
total 0
drwxr-xr-x   3 trudge  staff   96 Nov 22 13:02 .
drwx------@ 10 trudge  staff  320 Nov 22 12:21 ..
-rw-r--r--   1 trudge  staff    0 Nov 22 13:02 FileOne

YES! You can see it's there, and has 0 bytes. Good job eh!

mv

move

This command acually performs as 2 functions:
• it moves file/s to another location
• it renames file/s

What's the difference between copy and move?

copy COPIES a file to another location, so the original still exists
move MOVES a file to another location, so the original file is no longer there

This is crucial to understand

So to move a file to another location, it's similar to cp:

2024-11-23 11:26:13
[~/Desktop] trudge: mv FileOne Third

Verify...

[~/Desktop] trudge: ls -al Third
total 0
drwxr-xr-x  3 trudge  staff   96 Nov 23 11:26 .
drwx------@ 9 trudge  staff  288 Nov 23 11:26 ..
-rw-r--r--  1 trudge  staff    0 Nov 22 12:21 FileOne

Renaming is also similar:

mv Third/FileOne Third/FileOneRenamed

Verify...

[~/Desktop] trudge: ls -al Third
total 0
drwxr-xr-x  3 trudge  staff   96 Nov 23 11:30 .
drwx------@ 9 trudge  staff  288 Nov 23 11:26 ..
-rw-r--r--  1 trudge  staff    0 Nov 22 12:21 FileOneRenamed

wildcards