Sponsored Content
Top Forums Shell Programming and Scripting Bash script deleting my files, and editing files in subdirectories question Post 302886812 by neutronscott on Monday 3rd of February 2014 06:58:27 PM
Old 02-03-2014
This would take some work to keep them in subdirs. I see you already use the globstar ./** for recursion but didn't include the shopt -s globstar
This is bash4 feature, so let's write bash code and use all of it's nice features:

Code:
#!/bin/bash

(( $# == 2 )) || {
        printf 'Usage: %s <prefix> <ext>\n' "$0"
        exit 1
}

prefix=$1
type=$2
number=1
shopt -s globstar

for file in ./**; do
        [ -f "$file" ] || continue # only "regular" files

        printf -v new '%s%03d.%s' "$prefix" $((number++)) "$type"

        # skip to next file if name conflict
        [ -f "$new" ] && continue
        [ -x "$file" ] && continue

        mv "$file" "$new"
        echo "$file -> $new"

done

Code:
mute@thedoctor:~/temp/TheGreatGizmo$ touch IMG_{0000..0005}.jpg more/IMG_{000..0005}.jpg
mute@thedoctor:~/temp/TheGreatGizmo$ ls
IMG_0000.jpg  IMG_0001.jpg  IMG_0002.jpg  IMG_0003.jpg  IMG_0004.jpg  IMG_0005.jpg  more  script
mute@thedoctor:~/temp/TheGreatGizmo$ ./script
Usage: ./script <prefix> <ext>
mute@thedoctor:~/temp/TheGreatGizmo$ ./script file jpg
./IMG_0000.jpg -> file001.jpg
./IMG_0001.jpg -> file002.jpg
./IMG_0002.jpg -> file003.jpg
./IMG_0003.jpg -> file004.jpg
./IMG_0004.jpg -> file005.jpg
./IMG_0005.jpg -> file006.jpg
./more/IMG_0000.jpg -> file007.jpg
./more/IMG_0001.jpg -> file008.jpg
./more/IMG_0002.jpg -> file009.jpg
./more/IMG_0003.jpg -> file010.jpg
./more/IMG_0004.jpg -> file011.jpg
./more/IMG_0005.jpg -> file012.jpg
mute@thedoctor:~/temp/TheGreatGizmo$ ls .
file001.jpg  file003.jpg  file005.jpg  file007.jpg  file009.jpg  file011.jpg  more
file002.jpg  file004.jpg  file006.jpg  file008.jpg  file010.jpg  file012.jpg  script

These 2 Users Gave Thanks to neutronscott For This Post:
 

10 More Discussions You Might Find Interesting

1. Solaris

editing files with script

hi guys, We have to implement new local (/etc/default/login) USER security policy on almost 50 stations. so editing /etc/default/login and /etc/default/passwd will be way too long work. Can we do the same using some script, I mean editing the above files and putting variables as RETRIES=3, ... (5 Replies)
Discussion started by: Asteroid
5 Replies

2. Shell Programming and Scripting

Script for editing column files

Hi, I have been doing files editing (using "vi") to change the period (e.g. 0902 to 0903) for 100 files each week as shown in the example below. I would like to have a script to solve this manual files editing. Please advice. Example: Change from: ... (2 Replies)
Discussion started by: JunZ
2 Replies

3. Web Development

bash script editing my apache config files

okay i'm going to try to say this uber-simple: I use dropbox (file-sync service). in order for dropbox sync files, they must be its children eg. somewhere under /home/jzacsh/Dropbox]. I want to now use it to keep my development files in sync across my machines: easy: just move my dev. files... (2 Replies)
Discussion started by: jzacsh
2 Replies

4. Shell Programming and Scripting

Bash: Gzip files in Directory and itīs Subdirectories

Hello dear Community, I have a task to wrtie a script which will gzip not zipped files in a directory and itīs subdirectories. I succeeded in gzippung the directory but not the subdirectories: #/bin/bash #go to the directory where to zip cd $1 #Zip unzipped files for i in `ls | xargs... (2 Replies)
Discussion started by: JamesCarter
2 Replies

5. Shell Programming and Scripting

Editing lists of integers in 1d files with bash shell

Hi, I need a script that will: 1. Go through about 20 different folders, each containing about 20 1d files. The 1d files go something like this: 22.253 37.707 78.117 112.374 127.944 156.067 180.956 233.785 249.256 ... (1 Reply)
Discussion started by: ac130pilot
1 Replies

6. Shell Programming and Scripting

Script to Archive Files from Subdirectories

Hello, I have a CentOS server that contains a 'storage' directory. Within that directory, there could be any number of subfolders (all with unique names that match usernames). Under each username folder, there are two additional folders: db and files /STORAGE/user1/db/... (3 Replies)
Discussion started by: JasonH
3 Replies

7. UNIX for Dummies Questions & Answers

Crontab deleting files command question

Hello out there, Our system has a pdf generator that creates pdf files. We dont need them pas 120 days. So I have this command in my crontab. I currently set it to "0" for testing. But normally have it set to -mtime 120 to remove files out of the folders from PDF out to several other potential... (7 Replies)
Discussion started by: vsekvsek
7 Replies

8. Shell Programming and Scripting

Unzip all the files with subdirectories present and append a part of string from the main .zip files

Hi frnds, My requirement is I have a zip file with name say eg: test_ABC_UH_ccde2a_awdeaea_20150422.zip within that there are subdirectories on each directory we again have .zip files and in that we have files like mama20150422.gz and so on. Iam in need of a bash script so that it unzips... (0 Replies)
Discussion started by: Ravi Kishore
0 Replies

9. Shell Programming and Scripting

Append string to all the files inside a directory excluding subdirectories and .zip files

Hii, Could someone help me to append string to the starting of all the filenames inside a directory but it should exclude .zip files and subdirectories. Eg. file1: test1.log file2: test2.log file3 test.zip After running the script file1: string_test1.log file2: string_test2.log file3:... (4 Replies)
Discussion started by: Ravi Kishore
4 Replies

10. UNIX for Beginners Questions & Answers

Help with running a script on files located in subdirectories

Hello everyone, I'm am a newbie to coding so I am reaching out in hopes that I can get some help from this forum. I am trying to run the script below from a single directory, however the directory has many subfolders. In each of those subfolders is a file, uniquely named to that specific... (3 Replies)
Discussion started by: azurite
3 Replies
SYSPROFILE(8)						      System Manager's Manual						     SYSPROFILE(8)

NAME
sysprofile - modular centralized shell configuration DESCRIPTION
sysprofile is a generic approach to configure shell settings in a modular and centralized way mostly aimed at avoiding work for lazy sysad- mins. It has only been tested to work with the bash shell. It basically consists of the small /etc/sysprofile shell script which invokes other small shell scripts having a .bash suffix which are contained in the /etc/sysprofile.d/ directory. The system administrator can drop in any script he wants without any naming convention other than that the scripts need to have a .bash suffix to enable automagic sourcing by /etc/sysprofile. This mechanism is set up by inserting a small shell routine into /etc/profile for login shells and optionally into /etc/bashrc and/or /etc/bash.bashrc for non-login shells from where the actual /etc/sysprofile script is invoked: if [ -f /etc/sysprofile ]; then . /etc/sysprofile fi For using "sysprofile" under X11, one can source it in a similar way from /etc/X11/Xsession or your X display manager's Xsession file to provide the same shell environment as under the console in X11. See the example files in /usr/share/doc/sysprofile/ for illustration. For usage of terminal emulators with a non-login bash shell under X11, take care to enable sysprofile via /etc/bash.bashrc. If not set this way, your terminal emulators won't come up with the environment defined by the scripts in /etc/sysprofile.d/. Users not wanting /etc/sysprofile to be sourced for their environment can easily disable it's automatic mechanism. It can be disabled by simply creating an empty file called $HOME/.nosysprofile in the user's home directory using e.g. the touch(1) command. Any single configuration file in /etc/sysprofile.d/ can be overridden by any user by creating a private $HOME/.sysprofile.d/ directory which may contain a user's own version of any configuration file to be sourced instead of the system default. It's names have just to match exactly the system's default /etc/sysprofile.d/ configuration files. Empty versions of these files contained in the $HOME/.syspro- file.d/ directory automatically disable sourcing of the system wide version. Naturally, users can add and include their own private script inventions to be automagically executed by /etc/sysprofile at login time. OPTIONS
There are no options other than those dictated by shell conventions. Anything is defined within the configuration scripts themselves. SEE ALSO
The README files and configuration examples contained in /etc/sysprofile.d/ and the manual pages bash(1), xdm(1x), xdm.options(5), and wdm(1x). Recommended further reading is everything related with shell programming. If you need a similar mechanism for executing code at logout time check out the related package syslogout(8) which is a very close compan- ion to sysprofile. BUGS
sysprofile in its current form is mainly restricted to bash(1) syntax. In fact it is actually a rather embarrassing quick and dirty hack than anything else - but it works. It serves the practical need to enable a centralized bash configuration until something better becomes available. Your constructive criticism in making this into something better" is very welcome. Before i forget to mention it: we take patches... ;-) AUTHOR
sysprofile was developed by Paul Seelig <pseelig@debian.org> specifically for the Debian GNU/Linux system. Feel free to port it to and use it anywhere else under the conditions of either the GNU public license or the BSD license or both. Better yet, please help to make it into something more worthwhile than it currently is. SYSPROFILE(8)
All times are GMT -4. The time now is 12:22 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy