Shell Command to Create a text file in all sub-directories - linux

Hi everyone is there any linux command to create a custom text file in current directory and its sub directories
For example,
I would like to turn this directory tree:
.
├── 1
│ ├── A
│ └── B
├── 2
│ └── A
└── 3
├── A
└── B
└── I
9 directories, 0 files
into this
.
├── 1
│ ├── A
│ │ └── downloadedFrom.txt
│ ├── B
│ │ └── downloadedFrom.txt
│ └── downloadedFrom.txt
├── 2
│ ├── A
│ │ └── downloadedFrom.txt
│ └── downloadedFrom.txt
├── 3
│ ├── A
│ │ └── downloadedFrom.txt
│ ├── B
│ │ ├── downloadedFrom.txt
│ │ └── I
│ │ └── downloadedFrom.txt
│ └── downloadedFrom.txt
└── downloadedFrom.txt
9 directories, 10 files
i want to make a txt file in all directories (txt file content: my website name) is there any way?
or is there any way to overwrite files content in sub directories

Using only a single find command:
find . -type d -exec sh -c 'echo "yourwebsite" > "$1"' _ {}/downloadedFrom.txt \;
or, alternatively, using bare bash with globstar option (since bash 4):
shopt -s globstar
for dir in . **/; do echo "yourwebsite" > "$dir/downloadedFrom.txt"; done

for dir in `find . -type d` ; do echo "mywebsite.com" > $dir/downloadedfrom.txt ; done
Is this what you are looking for, sort of? The find command iterates through all directory type items in the current directory, and then we echo the contents of the file to a new file in all those directories. However, this will overwrite stuff that's already in those directories, so watch out!

Related

How do you remove an entire directory except for certain subdirectories in linux?

Suppose I run the following command in linux:
$ mkdir -p mp3 jpeg/dir1 jpeg/dir2 txt
$ touch mp3/1.mp3 mp3/2.mp3 mp3/3.mp3
$ touch jpeg/1.jpeg jpeg/2.jpeg jpeg/3.jpeg
$ touch txt/1.txt txt/2.txt txt/3.txt
This will create a directory structure like:
├── jpeg
│ ├── 1.jpeg
│ ├── 2.jpeg
│ └── 3.jpeg
│ └── dir1
│ └── dir2
├── mp3
│ ├── 1.mp3
│ ├── 2.mp3
│ └── 3.mp3
└── txt
├── 1.txt
├── 2.txt
└── 3.txt
How do I invoke the linux "rm" command to remove everything in the "jpeg" directory except for "dir2" subdirectory?
So I'm looking for a command that looks something like:
rm -rf -not dir2 jpeg
But when I run that command on Centos 7, I get the following error message:
rm: invalid option -- 'n'
My target output directory structure should look like:
├── jpeg
│
│
│
│
│ └── dir2
├── mp3
│ ├── 1.mp3
│ ├── 2.mp3
│ └── 3.mp3
└── txt
├── 1.txt
├── 2.txt
└── 3.txt
Would appreciate all/any help from the linux scripting community
You can use this find command to delete everything in jpeg directory except dir2:
find jpeg -mindepth 1 -not -path 'jpeg/dir2' -prune -delete

Copy multiple directories to another multiple directories from Linux shell [duplicate]

This question already has answers here:
How to copy a file to multiple directories using the gnu cp command
(22 answers)
Closed 9 months ago.
I want to copy several directories to another directories. How do I do it from the shell command prompt? for example:
Project
├── directory1
│ └── files1
├── directory2
│ └── files2
└── directory3
└── files3
to :
Project
├── directory1
│ └── files1
├── directory2
│ └── files2
├── directory3
│ └── files3
├── directory1.copy
│ └── files1
├── directory2.copy
│ └── files2
└── directory3.copy
└── files3
tried this:
mkdir directory{1..3}.copy
cp -r directory{1..3} directory{1..3}.copy
but all directories (and files inside) copy in directory3.copy
Indeed, cp just accepts all arguments before the first as sources and the last one as destination. If you want to copy to multiple places, you need a loop.
for dir in ./*/. # or for dir in directory{1..3}
do
cp -r "$dir" "$dir.copy"
done

tar folder and exclude all subfolders, then tar to specific path

Is there a tar command to exclude sub-directories but include every file in the root of the folder and then place them at specific path?
I would like to omit every subdir, and only keep files max-min depth = 1
I have tried the follwing but its not working:
tar -cvf r.tar --exclude='~/root-dir/[subdir, ... ]' ~/Desktop
root-dir/
|
├── subdir/
│ ├── subdir/
│ │ └── a.txt
│ |
│ ├── subdir/
│ │ └── b.txt
│ │
│ └── c.txt
├── subdir/
│ ├── subdir/
│ │ └── d.txt
│ │
│ ├── subdir/
│ │ └── subdir/
│ │ └── subdir/
| | └── subdir/...
|
|
└── apple.txt
└── banana.image
└── ...
first, use find to find the files meeting your criteria:
find ~/Desktop -type f -maxdepth 1
then pipe it to tar, using -T ( or --files-from) to tell tar to get the list of files from stdin:
find ~/Desktop -type f -maxdepth 1 | \
tar -T - cvf r.tar

Unzip 1million+ archives into correct folder structure

Firstly I am completely new to Linux as I set up an AWS Ubuntu instance for this project so be kind.
I have downloaded approximately 1 million .zip files containing .csv's in the following folder structure (financial data):
Main Folder
├── Exchange1
│ ├── Pair1
│ │ └── Month
│ │ └── .Zips
│ └── PairN
│ └── Month
│ └── .Zips
└── ExchangeN
├── Pair1
│ └── Month
│ └── .Zips
├── Pair2
│ └── Month
│ └── .Zips
└── PairN
└── Month
└── .Zips
and I would like to extract every zip under it's parent Pair folder disregarding the month folder so that the new structure would look like this:
Main Folder
├── Exchange1
│ ├── Pair1
│ │ └── Extracted .csv's
│ └── PairN
│ └── Extracted .csv's
└── ExchangeN
├── Pair1
│ └── Extracted .csv's
├── Pair2
│ └── Extracted .csv's
└── PairN
└── Extracted .csv's
Appreciate any help, thanks.
Run this script in your main folder.
#! /bin/sh
#find all nested zip files and assign it to an array
files=( $(find . -iname "*zip*" -exec echo {} \;) )
for i in "${files[#]}"
do
#extract the path to unzip the archive
path=$(echo "$i" | cut -d '/' -f-3)
unzip $i -d $path
done
Please be careful when you run this. Assigning the output of ls/find to an array can have weird and unexpected consequences when the directory contains filenames with a newline, pipe etc.
Quoting Greg
Unix allows almost any character in a filename, including whitespace, newlines, commas, pipe symbols, and pretty much anything else you'd ever try to use as a delimiter except NUL.
Reference
1. Why you shouldn't parse the output of ls
The simplest thing which might possibly work is:
find . -iname "*.zip" -execdir unzip -d ../ {} ";"
issued from Main Folder/.
But first try with an echo for visual control:
find ./Exchange1/Pair1 -iname "*.zip" -execdir echo unzip -d ../ {} ";"
If this looks promising, make a copy of some test folders and try over there:
find ./Exchange1/ -iname "*.zip" -execdir unzip -d ../ {} ";"
If it works, take the real files.

Linux: Batch rename multiple files to parent dir + suffix in order of name

I need to batch rename multiple images and want to use the parent directory as base name. To prevent one overwriting the other, a suffix must be added. The order of the renaming process musts follow the timestamp of the file. Because the 'first' file is a featured image for the site I'm using it for.
Tree:
└── main
├── white tshirt
│   ├── IMG_1.jpg
│   ├── IMG_2.jpg
│   ├── IMG_3.jpg
│   └── IMG_4.jpg
├── black tshirt
│   ├── IMG_1.jpg
│   ├── IMG_2.jpg
│   ├── IMG_3.jpg
│   └── IMG_4.jpg
└── red tshirt
├── IMG_1.jpg
├── IMG_2.jpg
├── IMG_3.jpg
└── IMG_4.jpg
Goal:
└── main
├── white tshirt
│   ├── white-tshirt-1.jpg
│   ├── white-tshirt-2.jpg
│   ├── white-tshirt-3.jpg
│   └── white-tshirt-4.jpg
├── black tshirt
│   ├── black-tshirt-1.jpg
│   ├── black-thisrt-2.jpg
│   ├── black-tshirt-3.jpg
│   └── black-tshirt-4.jpg
└── red tshirt
├── red-tshirt-1.jpg
├── red-tshirt-2.jpg
├── red-tshirt-3.jpg
└── red-tshirt-4.jpg
Replacing spaces with dashes is not required, but preferred. Platform: Debian 8
I think this should do the job:
#!/bin/sh
for dir in *; do
if [ ! -d "$dir" ]; then
continue
fi
cd "$dir"
pref=$(echo "$dir" | tr ' ' -)
i=1
ls -tr | while read f; do
ext=$(echo "$f" | sed 's/.*\.//')
mv "$f" "${pref}-${i}.$ext"
i=$(expr $i + 1)
done
cd ..
done
Invoke the script inside your main directory and make sure there are only your target folders in it. Also make sure your files'names do not contain the character '\'

Resources