Copy files and create directories in linux

How to create, copy, move, and remove files and directories in Linux

To manage files, you need to be able to create, remove, copy, and move them. You also need to organize them logically into directories, which you also need to be able to create, remove, copy, and move. The following table summarizes some of the most common file management commands. The remainder of this section will discuss ways to use these commands in more detail.

ACTIVITY COMMAND SYNTAX
Create a directory mkdir directory
Copy a file cp file new-file
Copy a directory and its contents cp -r directory new-directory
Move or rename a file or directory mv file new-file
Remove a file rm file
Remove a directory containing files rm -r directory
Remove an empty directory rmdir directory

Creating Directories

The mkdir command creates one or more directories or subdirectories. It takes as arguments a list of paths to the directories you want to create. The mkdir command will fail with an error if the directory already exists, or if you are trying to create a subdirectory in a directory that does not exist. The -p (parent) option creates missing parent directories for the requested destination. Use the mkdir -p command with caution, because spelling mistakes can create unintended directories without generating error messages.

In the following example, pretend that you are trying to create a directory in the Videos directory named Watched, but you accidentally left off the letter “s” in Videos in your mkdir command.

[[email protected] ~]$ mkdir Video/Watched mkdir: cannot create directory `Video/Watched': No such file or directory 

The mkdir command failed because Videos was misspelled and the directory Video does not exist. If you had used the mkdir command with the -p option, the directory Video would be created, which was not what you had intended, and the subdirectory Watched would be created in that incorrect directory.

After correctly spelling the Videos parent directory, creating the Watched subdirectory will succeed.

 [[email protected] ~]$ mkdir Videos/Watched [[email protected] ~]$ ls -R Videos Videos/: blockbuster1.ogg blockbuster2.ogg Watched Videos/Watched: 

In the following example, files and directories are organized beneath the /home/user/ Documents directory. Use the mkdir command and a space-delimited list of the directory names to create multiple directories.

[[email protected] ~]$ cd Documents [[email protected] Documents]$ mkdir ProjectX ProjectY [[email protected] Documents]$ ls ProjectX ProjectY 

Use the mkdir -p command and space-delimited relative paths for each of the subdirectory names to create multiple parent directories with subdirectories.

[[email protected] Documents]$ mkdir -p Thesis/Chapter1 Thesis/Chapter2 Thesis/Chapter3 [[email protected] Documents]$ cd [[email protected] ~]$ ls -R Videos Documents Documents: ProjectX ProjectY Thesis Documents/ProjectX: Documents/ProjectY: Documents/Thesis: Chapter1 Chapter2 Chapter3 Documents/Thesis/Chapter1: Documents/Thesis/Chapter2: Documents/Thesis/Chapter3: Videos: blockbuster1.ogg blockbuster2.ogg Watched Videos/Watched: 

The last mkdir command created three ChapterN subdirectories with one command. The -p option created the missing parent directory Thesis.

Читайте также:  Source lines of code linux

Copying Files

The cp command copies a file, creating a new file either in the current directory or in a specified directory. It can also copy multiple files to a directory.

WARNING: If the destination file already exists, the cp command overwrites the file.

[[email protected] ~]$ cd Videos/ [[email protected] Videos]$ cp blockbuster1.ogg blockbuster3.ogg [[email protected] Videos]$ ls -l total 0 -rw-rw-r--. 1 user user 0 Oct 3 04:47 blockbuster1.ogg -rw-rw-r--. 1 user user 0 Oct 3 04:47 blockbuster2.ogg -rw-rw-r--. 1 user user 0 Oct 3 04:47 blockbuster3.ogg [[email protected] Videos]$ 

When copying multiple files with one command, the last argument must be a directory. Copied files retain their original names in the new directory. If a file with the same name exists in the target directory, the existing file is overwritten. By default, the cp does not copy directories; it ignores them. In the following example, two directories are listed, Thesis and ProjectX. Only the last argument, ProjectX is valid as a destination. The Thesis directory is ignored.

[[email protected] Videos]$ cd ../Documents [[email protected] Documents]$ cp thesis_chapter1.odf thesis_chapter2.odf Thesis ProjectX cp: omitting directory `Thesis' [[email protected] Documents]$ ls Thesis ProjectX ProjectX: thesis_chapter1.odf thesis_chapter2.odf Thesis: Chapter1 Chapter2 Chapter3 

In the first cp command, the Thesis directory failed to copy, but the thesis_chapter1.odf and thesis_chapter2.odf files succeeded. If you want to copy a file to the current working directory, you can use the special . directory:

[[email protected] ~]$ cp /etc/hostname . [[email protected] ~]$ cat hostname host.example.com 

Use the copy command with the -r (recursive) option, to copy the Thesis directory and its contents to the ProjectX directory.

 [[email protected] Documents]$ cp -r Thesis ProjectX [[email protected] Documents]$ ls -R ProjectX ProjectX: Thesis thesis_chapter1.odf thesis_chapter2.odf ProjectX/Thesis: Chapter1 Chapter2 Chapter3 ProjectX/Thesis/Chapter1: ProjectX/Thesis/Chapter2: thesis_chapter2.odf ProjectX/Thesis/Chapter3: 

Moving Files

The mv command moves files from one location to another. If you think of the absolute path to a file as its full name, moving a file is effectively the same as renaming a file. File contents remain unchanged. Use the mv command to rename a file.

[[email protected] Videos]$ cd ../Documents [[email protected] Documents]$ ls -l thesis* -rw-rw-r--. 1 user user 0 Feb 6 21:16 thesis_chapter1.odf -rw-rw-r--. 1 user user 0 Feb 6 21:16 thesis_chapter2.odf [[email protected] Documents]$ mv thesis_chapter2.odf thesis_chapter2_reviewed.odf [[email protected] Documents]$ ls -l thesis* -rw-rw-r--. 1 user user 0 Feb 6 21:16 thesis_chapter1.odf -rw-rw-r--. 1 user user 0 Feb 6 21:16 thesis_chapter2_reviewed.odf 

Use the mv command to move a file to a different directory.

[[email protected] Documents]$ ls Thesis/Chapter1 [[email protected] Documents]$ [[email protected] Documents]$ mv thesis_chapter1.odf Thesis/Chapter1 [[email protected] Documents]$ ls Thesis/Chapter1 thesis_chapter1.odf [[email protected] Documents]$ ls -l thesis* -rw-rw-r--. 1 user user 0 Feb 6 21:16 thesis_chapter2_reviewed.odf 

Removing Files and Directories

The rm command removes files. By default, rm will not remove directories that contain files, unless you add the -r or –recursive option.

Читайте также:  Nvidia quadro для linux

Note: There is no command-line undelete feature, nor a “trash bin” from which you can restore files staged for deletion.

It is a good idea to verify your current working directory before removing a file or directory.

[[email protected] Documents]$ pwd /home/geek/Documents 

Use the rm command to remove a single file from your working directory.

[[email protected] Documents]$ ls -l thesis* -rw-rw-r--. 1 user user 0 Feb 6 21:16 thesis_chapter2_reviewed.odf [[email protected] Documents]$ rm thesis_chapter2_reviewed.odf [[email protected] Documents]$ ls -l thesis* ls: cannot access 'thesis*': No such file or directory 

If you attempt to use the rm command to remove a directory without using the -r option, the command will fail.

[[email protected] Documents]$ rm Thesis/Chapter1 rm: cannot remove `Thesis/Chapter1': Is a directory 

Use the rm -r command to remove a subdirectory and its contents.

[[email protected] Documents]$ ls -R Thesis Thesis/: Chapter1 Chapter2 Chapter3 Thesis/Chapter1: thesis_chapter1.odf Thesis/Chapter2: thesis_chapter2.odf Thesis/Chapter3: [[email protected] Documents]$ rm -r Thesis/Chapter1 [[email protected] Documents]$ ls -l Thesis total 8 drwxrwxr-x. 2 user user 4096 Feb 11 12:47 Chapter2 drwxrwxr-x. 2 user user 4096 Feb 11 12:48 Chapter3 

The rm -r command traverses each subdirectory first, individually removing their files before removing each directory. You can use the rm -ri command to interactively prompt for confirmation before deleting. This is essentially the opposite of using the -f option, which forces the removal without prompting the user for confirmation.

[[email protected] Documents]$ rm -ri Thesis rm: descend into directory `Thesis'? y rm: descend into directory `Thesis/Chapter2'? y rm: remove regular empty file `Thesis/Chapter2/thesis_chapter2.odf'? y rm: remove directory `Thesis/Chapter2'? y rm: remove directory `Thesis/Chapter3'? y rm: remove directory `Thesis'? y 

WARNING: If you specify both the -i and -f options, the -f option takes priority and you will not be prompted for confirmation before rm deletes files.

In the following example, the rmdir command only removes the directory that is empty. Just like the earlier example, you must use the rm -r command to remove a directory that contains content.

[[email protected] Documents]$ pwd /home/geek/Documents [[email protected] Documents]$ rmdir ProjectY [[email protected] Documents]$ rmdir ProjectX rmdir: failed to remove `ProjectX': Directory not empty [[email protected] Documents]$ rm -r ProjectX [[email protected] Documents]$ ls -lR .: total 0 [[email protected] Documents]$ 

Note: The rm command with no options cannot remove an empty directory. You must use the rmdir command, rm -d (which is equivalent to rmdir), or rm -r.

Источник

How to create a directory and copy files into it with bash

I have some files in Pictures\ with extension *.png and directories like 12-21-20, 12-20-20. These directories was created with dir= mkdir $(date +’%m’-‘%d’-‘%Y’) At the end of the day I want to run a script which will create a folder $dir and copy all png files I’ve made for today into that folder. How can I do that? Any information you could give me would be greatly appreciated.

Читайте также:  Linux get users info

1 Answer 1

date +’%m-%d-%Y’ is the date command outputting, e,g. 12-22-2020 . $(..) is called command substitution that captures the result of the date command allowing it to be assigned to the variable dir .

To create a directory with the contents of $dir (e.g. 12-22-2020 ) you would use the mkdir command, providing the -p option to suppress the error if that directory already exists (and also create parent directories as necessary — not relevant here). You want to ensure it succeeds before you attempt to copy files to the new directory, so you would use:

Which simply exits if the command fails.

At this point, you can simply use cp (or preferably mv to move the files) from whatever source directory they currently reside in. That you can do with:

mv /path/to/source/dir/*.png "$dir" 
cp -a /path/to/source/dir/*.png "$dir" 

Both cp -a and mv will preserve the original file attributes (time, date, permissions, etc. ).

From a script standpoint, you will either want to change to the directory above the new «$dir» or use the full path, e.g.

mv /path/to/source/dir/*.png "/path/to/$dir" 

Short Example

If you want to provide the directory containing the .png files to move to «$dir» created with today’s date, you could write a short script like the following. You provide the directory containing the .png files you would like to copy or move as the first argument to the script on the command-line, e.g. usage would be bash pngscript.sh /path/to/source/dir .

#!/bin/bash [ -z "$1" ] && < ## validate one argument given for source directory printf "usage: ./%s /path/to/images" "$" >&2 exit 1 > [ "$(ls -1 "$1"/*.png | wc -l)" -gt 0 ] || < ## validate png files in source dir printf "error: no .png files in '%s'\n" "$1" >&2 exit 1 > dir=$(date +'%m-%d-%Y') ## get current date mkdir -p "$dir" || exit 1 ## create directory, exit on failure mv "$1"/*.png "$dir" ## move .png files from source to "$dir" 

(note: it will create the «$dir» directory below the current working directory and then move files from the path provided as the first argument (positional parameter) to the newly created directory. Change mv to cp -a if you want to leave a copy of the files in the original directory)

You can make the script executable with chmod +x pngscript.sh and then you can simply run it from the current directory as:

./pngscript.sh /path/to/source/dir 

Let me know if you have further questions.

Источник

Оцените статью
Adblock
detector