Remove only one specified directory recursively from a folder (Linux) - linux

My question is pretty quick to explain, I don't know about the solution.
I have a folder always containing 3 folders that can themself contained others folder. I would like to be able to remove only a specific folder from all 3 firsts folders. Here how I picture the thing :
For example here I would like with a single command to remove all folder named Folder 1 under the User folder (if it's possible, if not using a shell script could do the trick)
> User_folder
> Folder_A
> Folder_1 --> To remove
> Folder_2
> Folder_3
> Folder_4
> Folder_B
> Folder_1 --> To remove
> Folder_5
> Folder_6
> Folder_7
> Folder_C
> Folder_1 --> To remove
> Folder_2
> Folder_4
> Folder_5

Related

Is it possible to move files within a zip file? [closed]

Closed. This question is not about programming or software development. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 5 months ago.
Improve this question
example.zip/
└── example/
├── nice.md
├── tree.md
└── diagram.md
Expected:
example.zip/
├── nice.md
├── tree.md
└── diagram.md
example.zip contains a folder with the same name. In it are files that I want to move to the root of the zip file and remove the empty directory.
I looked at the zip man page. Could not find any flags related to the issue or I could be missing something.
I tried the --copy-entries flag. This create a new zip with selected files from the existing zip but also copy over the folder hierarchy.
zip example.zip "*.md" --copy-entries --out example1.zip
I am trying to write a shell script to do this.
Is it possible to do without extracting the zip?
If you have (or can install) 7z (aka p7zip) you can make use of the d(delete) and rn(rename) options, eg:
$ mkdir example
$ touch example/{nice.md,tree.md,diagram.md}
$ zip -r example.zip example
adding: example/ (stored 0%)
adding: example/diagram.md (stored 0%)
adding: example/nice.md (stored 0%)
adding: example/tree.md (stored 0%)
$ unzip -l example.zip
Archive: example.zip
Length Date Time Name
--------- ---------- ----- ----
0 09-15-2022 09:29 example/
0 09-15-2022 09:29 example/diagram.md
0 09-15-2022 09:29 example/nice.md
0 09-15-2022 09:29 example/tree.md
--------- -------
0 4 files
# rename the *.md files first and then delete the directory; if you delete
# the directory first you'll lose all files under the directory; the 7z d/rn
# commands will generate a lot of output (not shown here)
$ 7z rn example.zip example/nice.md nice.md
$ 7z rn example.zip example/tree.md tree.md
$ 7z rn example.zip example/diagram.md diagram.md
$ 7z d example.zip example
$ unzip -l example.zip
Archive: example.zip
Length Date Time Name
--------- ---------- ----- ----
0 09-15-2022 09:29 diagram.md
0 09-15-2022 09:29 nice.md
0 09-15-2022 09:29 tree.md
--------- -------
0 3 files
$ unzip example.zip
Archive: example.zip
extracting: diagram.md
extracting: nice.md
extracting: tree.md
I'm guessing in OP's real life example the names of the directories and/or files may not be known in advance; the 7z commands do work with bash variables (eg, 7z d "${zipfile}" "${dir_to_delete}"); if OP has issues dynamically processing the contents of a given *zip then I'd recommend asking a new question ...
For a large number of renames (or deletes) it looks like you can also:
specify multiple source/destination pairs on the single command line
use a list file
Good answer. Just to be clear, 7z does not do an in-place edit on the zip file when it does the rename/delete. Under the hood it copies the old zip into a temporary file (example.zip.tmp in this instance), renaming & deleting as it does that copy. Then it deletes the original zip file and renames the temporary file, example.zip.tmp back to original filename, example.zip. For the most part this is a perfectly acceptable (and safe) approach.
Here are the relevant lines from an strace run that shows the deletion of th eoriginal example.zip file, followed by renaming the example.zip.tmp file to example.zip.
$ strace 7z rn example.zip example/tree.md tree.md
...
unlink("example.zip") = 0
rename("example.zip.tmp", "example.zip") = 0
...
Main edge condition of this approach is with very large zip files where you are strapped for disk space -- you need to have space available to store the zip file twice when it creates the temporary copy.

How to unzip archive directly into target folder without creating a subfolder with the archive name (7zip, command line)?

I'm using the 7zip command line interface to extract archives, like so:
7za.exe x -y {path_to_zipfile} -o{path_to_target_folder}
If my zipfile is named my_archive.7z, then I get the following filestructure in the target folder:
🗁 target_folder
└─ 🗁 my_archive
├─ 🗋 foo.png
├─ 🗁 bar
│ ├─ 🗋 baz.txt
│ └─ 🗋 qux.txt
...
However, I don't want the subfolder 🗁 my_archive. I'm looking for flags to apply on the 7zip command such that everything extracts directly in the target folder, without creating the 🗁 my_archive subfolder.
NOTES
I can't replace x with e because the filestructure shouldn't be lost (the e flag pushes all files to the toplevel).
I'm working on a Windows 10 computer, but the solution must also work on Linux.
I'm using the following version: 7-Zip (a) 19.00 (x64)
Some background info: I'm calling 7zip from a Python program, like so:
# Variables:
# 'sevenzip_abspath': absolute path to 7za executable
# 'zipfile_abspath': absolute path to zipped file (`.7z` format)
# 'targetdir_abspath': absolute path to target directory
commandlist = [
sevenzip_abspath,
'x',
'-y',
zipfile_abspath,
f'-o{targetdir_abspath}',
]
output = subprocess.Popen(
commandlist,
stdout=subprocess.PIPE,
shell=False,
).communicate()[0]
if output is not None:
print(output.decode('utf-8'))
I know I could do all kinds of things in Python after the unzipping has finished (move/rename directories, etc etc), but that's for plan B. First I want to check if there is an elegant solution.
I'd like to stick to 7zip for reasons that would lead us too far here.
You can rename the top level folder to match the target folder before extracting the archive.
7za rn {path_to_zipfile} my_archive target_folder
This will permanently change the archive. If you don't want that, take a copy first.

Create a text file containing list of (relative paths to) files in a directory?

Suppose I am standing on a directory. Inside there's another directory called inside_dir containing a huge number of files. I want to create a file containing a list of all the files inside inside_dir, listed as the relative path to the files. That is, if there is a file called file1 inside inside_dir, the corresponding line in the list file should be inside_dir/file1.
Since the number of files is huge, just doing ls inside_dir/* > list.txt won't work because it will complain about having too many arguments.
find inside_dir -type f > list.txt

Implement Archive/Backup for Liferay 6.2 Document Library

Need to implement archive/backup features for Liferay 6.2 Document Library with the AdvancedFileSystemStore as given below.
dl.store.impl=com.liferay.portlet.documentlibrary.store.AdvancedFileSystemStore
I have the two below scenarios. Please suggest if there are any Liferay OOB features that can be used to accomplish the below OR any pointers on how to achieve this using custom development.
Scenario 1:
Have only a single Site which has different folders containing various documents in Documents and Media Section. These folders need to be backed up periodically - say once every week. The logical folder hierarchy and the documents should be preserved in the archive folder.
For Example:
Sample Site 1 > Documents and Media > RootFolder > Folder1 > File1
Sample Site 1 > Documents and Media > RootFolder > Folder1 > File2
Sample Site 1 > Documents and Media > RootFolder > Folder2 > File1
Sample Site 1 > Documents and Media > RootFolder > Folder2 > File2
After backup this structure in archive directory should be:
ArchiveFolder > RootFolder > Folder1 > File1
ArchiveFolder > RootFolder > Folder1 > File2
ArchiveFolder > RootFolder > Folder2 > File1
ArchiveFolder > RootFolder > Folder2 > File2
Scenario 2:
Have multiple sites each with different folders containing various documents in their respective Documents and Media section. All these folders need to be backed up periodically - say once every week. The logical folder hierarchy and the documents should be preserved in the archive folder.
For Example:
Sample Site 1 > Documents and Media > RootFolder > Folder1 > File1
Sample Site 1 > Documents and Media > RootFolder > Folder1 > File2
Sample Site 2 > Documents and Media > RootFolder > Folder1 > File1
Sample Site 2 > Documents and Media > RootFolder > Folder1 > File2
After backup this structure in archive directory should be:
ArchiveFolder > Sample Site 1 > RootFolder > Folder1 > File1
ArchiveFolder > Sample Site 1 > RootFolder > Folder1 > File2
ArchiveFolder > Sample Site 2 > RootFolder > Folder1 > File1
ArchiveFolder > Sample Site 2 > RootFolder > Folder1 > File2
In my mind a good solution can be a custom portlet.
For defining folders you can choose between:
- user interface where administrator put source/destination folder id and group id
- a property file in which reading ids
For scheduled operation you can choose between:
- a scheduled operation (defined in liferay-portlet.xml)
- a service invoked by an automatic system cronjob
Working with files is very simple in Liferay (for visiting and copying any "node" in the Document Library tree structure): don't care about specific store.impl... using Documents API (available starting from LR 6.1) you will have a very powerful (and high-level) way to work on document library.

Proper use of Wildcard in Makefile

I have to use a makefile in order to copy all the contents of say Folder 1 into Folder 2. The contents of folder 1 are 5 files having names as abc_1.c,abc_2.c,.. and so on.
The paths of folders 1 & 2 are both given as variables in makefile and then the command for copy is being given.
The problem is that even after using Wildcard function I am able to copy the entire folder 1 into folder 2, and not the contents of folder1.
Kindly help me out by suggesting a method to copy the contents of folder 1 into 2 and not the entire folder.
Following is the code snippet which I used initially :
PATH_1 = /ace/1/*
PATH_2 = /ace/2/
The code to copy : shell cp -rf ("$(PATH_1)") ("$(PATH_2)")
Pls dont mind the above line of code.
The main problem was in PATH_1 as the '*' was not resolved properly and the contents were not copied. On removing the '*' from PATH_1 the code worked but again it copied entire folder 1 into folder 2.
Later, I modified Path_1 as follows :
PATH_1 = /ace/1/(wildcard abc_*.c)
Still the same thing, the entire folder 1 is being copied into folder 2.
Kindly help me out with this.
Try something on these lines:
PATH1=/ace/1
PATH2=/ace/2
C_SRCS=$(wildcard $(PATH1)/abc_*.c)
copy:
cp -vi ${C_SRCS} $(PATH2)
The copy options v is for verbose & i is for interactive, which you can ignore if you want to.
This rule will fail with error with C_SRCS is empty, to prevent that you can have a slightly robust rule something on these lines:
copy:
for C_SRC in ${C_SRCS}; do \
cp -vi $$C_SRC $(PATH2); \
done
Hope this helps!

Resources