Linux copy hidden file

How to copy hidden (starting with a dot) files and subdirectories in linux?

How to copy hidden files and hidden subdirectories (the ones starting with a dot) in folder A to folder B? For example if I have this structure:

A/a A/b A/.a A/.b/ A/.b/somefile A/.b/.c 

I have already tried this command: cp A/.* B from this other superuser question. However, it does not copy the subdirectories. Also tried cp -r A/.* B , but it copies . so I end with an exact copy of A (including the normal files). Any help is appreciated.

5 Answers 5

As long as you’re only looking for hidden files and folders at the level of A and don’t want, for example

to be copied, you should be able to use this:

It basically means copy anything that starts with a . and then any character other than a . That filters out . and ..

Edit: Removed the -p from the cp command since Asker hasn’t indicated he wants to preserve any ownerships, dates, etc.

This works for the example file and directory names given in the question, but the text of the question says “hidden files and hidden subdirectories (the ones starting with a dot)”, and this answer will not find files and directories whose names begin with two dots; e.g., ..c .

That’s quite an edge case, but a legitimate concern none-the-less. I hadn’t considered that. You could account for that by switching to .*[^.] but then you’d miss files that end with a . . I think you would indeed need extended globbing to truly account for all cases.

For cases like this would recommend using find instead of cp like this:

find A/ -type f -maxdepth 1 -name '.*' -exec cp -p <> B/ \; 

The basic syntax breaks down like this:

  • find A/ -type f : find items in the directory A/ whose type is a file (instead of a directory)…
  • -maxdepth 1 -name ‘.*’ : To this for a maxdepth of 1 directories and whose name begins with . .
  • -exec cp -p <> B/ \; : And once these files are found, exec the cp command with a -p flag to preserve dates/times from the source ( <> ) to the destination of B/ .

I like using maxdepth to add a layer of control so I am not accidentally copying a whole filesystem. But feel free to remove that.

The problem with A/.* is that there is the directory . in A which also matches the pattern.

You can turn on extended glob patterns and use the following:

It matches files whose name starts with a dot and whose second character is neither a dot nor nothing ( ?(.) matches nothing or a dot, !(. ) negates it, i.e. !(?(.)) matches everything else than nothing or a dot).

+1 for a correct answer. Note that .!(@(|.)) is (AFAICT) equivalent to the above, (IMNSHO) a little clearer, and only one character longer.

 for item in `find A -type d | grep -E "\."` ; do cp -r $item B ; done 
  • find A -type d provides a recursive list within A with only directories
  • grep -E «\.» filters directories with a dot (i.e.: hidden directories)
  • the -E option was needed here because without it it means «current directory» as well
  • the backslash is to avoid the meaning, under regexp, of «any character»
  • cp -r to copy recursively
Читайте также:  Ос linux основные элементы

I have created the files and folders structure for A and executed the command in Git Bash (I’m not with a linux just right now) and it worked.

Thanks for noticing 🙂 Actually I limitied to the «test case» by @gaboroncancio. If you can give me other test battery I may try to improve that (of course if you want, improve it by yourself either editing this response or creating a new answer)

You could simply put the dotfiles in a folder called A B , and then it’d act unexpectedly because it’d expand to cp -r A B/.dotfile B . The general advice is not to parse find or ls output at all. If you use find you should also use its own options for filtering rather than grep , and if you pipe find output somewhere else, use -print0 , or directly call the command you want. See the find manual.

Even more generally, when working with files it’s safest to use shell globs as explained in other answers (although they often require extglob to be set).

Источник

How to Copy Hidden Files in Linux

Sometimes, you might need to copy hidden files or folders from one location to another. In Linux, hidden files are characterized by a period (.) as the first character of their name. A common example would be copying a user’s home directory or a directory with a website, as these directories often contain many hidden files.

Everything will work smoothly when copying a single hidden file. However, issues arise when trying to copy multiple files using the * wildcard pattern. This pattern doesn’t account for the period (.) character, resulting in all hidden files being excluded from the copy process. This design choice was made to avoid unintentional recursion by skipping references to the current folder (.) and the parent folder (..).

Nonetheless, there are ways to circumvent this limitation. It is possible to copy the contents of a folder without using *. For example, let’s create an app folder with .htaccess and index.php files and a backup folder where you want to copy the files. You can use the same link to the current folder and then cp will copy all content, including hidden files there to the desired folder:

cp-r-app-sl-backup.png

Alternatively, you can also explicitly specify that you want to copy the contents of the folder passed in the first parameter by using the -T option:

cp-rt-app-backup.png

This command also requires the -r option, for recursive folder processing, as the first parameter specifies a folder, not a list of files.

Lastly, if you want to copy only hidden files from the directory you can use the following wildcard syntax:

cp-app-sl-backup.png

However, avoid adding the -r option to this command. If included, it will attempt to copy not only the specified directory but also all directories above it, including the system root, which can lead to unintended consequences.

Found a mistake in the text? Let me know about that. Highlight the text with the mistake and press Ctrl+Enter.

Читайте также:  Linux ftp изменить порт

Источник

How to copy hidden files in linux

Solution 1: As long as you’re only looking for hidden files and folders at the level of A and don’t want, for example to be copied, you should be able to use this: It basically means copy anything that starts with a and then any character other than a That filters out and Edit: Removed the -p from the cp command since Asker hasn’t indicated he wants to preserve any ownerships, dates, etc. Solution 3: The correct means of doing this is to use the option, and recursively copy the folders (without trailing slashes, asterisks, etc.), i.e.: This will copy the contents of to (including hidden files), creating the folder if it does not exist; however the option prevents the contents of from being copied to a new folder should the folder exist.

How to copy hidden (starting with a dot) files and subdirectories in linux?

As long as you’re only looking for hidden files and folders at the level of A and don’t want, for example

to be copied, you should be able to use this:

It basically means copy anything that starts with a . and then any character other than a . That filters out . and ..

Edit: Removed the -p from the cp command since Asker hasn’t indicated he wants to preserve any ownerships, dates, etc.

The problem with A/.* is that there is the directory . in A which also matches the pattern.

You can turn on extended glob patterns and use the following:

It matches files whose name starts with a dot and whose second character is neither a dot nor nothing ( ?(.) matches nothing or a dot, !(. ) negates it, i.e. !(?(.)) matches everything else than nothing or a dot).

For cases like this would recommend using find instead of cp like this:

find A/ -type f -maxdepth 1 -name '.*' -exec cp -p <> B/ \; 

The basic syntax breaks down like this:

  • find A/ -type f : find items in the directory A/ whose type is a file (instead of a directory)…
  • -maxdepth 1 -name ‘.*’ : To this for a maxdepth of 1 directories and whose name begins with . .
  • -exec cp -p <> B/ \; : And once these files are found, exec the cp command with a -p flag to preserve dates/times from the source ( <> ) to the destination of B/ .

I like using maxdepth to add a layer of control so I am not accidentally copying a whole filesystem. But feel free to remove that.

Linux — Bash shell copy files on the hidden directory, Teams. Q&A for work. Connect and share knowledge within a single location that is structured and easy to search. Learn more

Linux — how to copy all files including hidden using cp

How to copy all files recursively from one directory to another, including hidden files . Using cp command in Linux .http://linuxoidchannel.blogspot.com/2017/0

How to copy with cp to include hidden files and hidden directories and their contents?

(Note that /home/user must not exist already, or else it will create /home/user/skel .)

Lets say you created the new folder (or are going to create one) and want to copy the files to it after the folder is created

mkdir /home/ cp -r /etc/skel/. /home/

This will copy all files/folder recursively from /etc/skel in to the already existing folder created on the first line.

Читайте также:  Функция fork в linux

The correct means of doing this is to use the -T (—no-target-directory) option, and recursively copy the folders (without trailing slashes, asterisks, etc.), i.e.:

This will copy the contents of /etc/skel to /home/user (including hidden files), creating the folder /home/user if it does not exist; however the -T option prevents the contents of /etc/skel from being copied to a new folder /home/user/skel should the folder /home/user exist.

Cp — Copy hidden files in a directory to a directory without, The problem is that you don’t have write access to the directory, so it is not letting you copy files there. One of the answers suggests using Sudo, but you claimed it was not working. This is most likely because your user is not listed in the sudoers file, meaning that you do not have permission to use sudo on this …

How can I copy a hidden directory recursively and preserving its permissions?

Don’t specify the files or Directory

Lets say you created the new folder (or are going to create one) and want to copy the files to it after the folder is created

mkdir /test/folder cp -rp /path/to/copy/. /test/folder 

This will copy all files/folder recursively from /path/from/copy in to the already existing folder created on the first line.

Another approach is tar . For example:

$cd foo $tar cf - . | tar -C /path/to/bar -x 
mkdir backupcache cp -rp .cache/. backupcache 

that way only the content ( /. ) of .cache gets copied, not the .cache part.

How to Copy All Files from a Directory to another, To confirm, type: $ ls / home / wardah / dir2. To copy all the directory files to another location, the command would be: $ rsync -a / home / wardah / dir1 / / home / wardah / dir2. (The “ -a ” with the “ rsync ” command is used to copy directories recursively) Here are the two concepts:

Recursively copying hidden files

My favorite to move dirs in general has been:

tar cvf - . | (cd /dest/dir; tar xvf -) 

which tars up the current directory to stdout then pipes it to a subshell that first cd’s to the destination directory before untarring stdin. Simple, direct, extensible — consider what happens when you replace the () with an ssh to another machine. Or to answer your question you might do:

tar cvf - .* --exclude=\. --exclude=\.\. | (cd /dest/dir; tar xvf -) 

Almost every time this can be solved just with:

It’s pretty unusual to have a hidden file that doesn’t start with one of those characters.

Other pattern matches are available ( . * , .[^.]* ) — see the comments

rsync -a ./ /some/other/directory/ 

that will copy the contents of the current directory (including dot files, but not including .. )

How To View and Create Hidden Files in Linux, Method 2: GUI (Graphical User Interface) Step 1: Right-click on the file and select the Rename option. Step 2: Make the file hidden by placing a . (dot) at the beginning of the filename. Renaming the file by clicking ‘Rename’ and adding . (dot) at the beginning of the new file name will hide the file.

Источник

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