Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Finding and renaming files with exceptions Post 302970204 by Don Cragun on Sunday 3rd of April 2016 09:34:28 PM
Old 04-03-2016
Quote:
Originally Posted by azurite
Could you explain the syntax of the find code you have provided? I had searched google and used the following which is a bit different from what you have and I am wondering if you could explain the difference so I can understand it better? find . -name '*eddy_corrected_brain.nii.gz*'



Ahh I see now. Sorry I'm still figuring out Linux and how things are referred to in this environment. I think the work computer uses bash.



Okay so to make sure I understand correctly, if I use find . -name '*eddy_corrected*nii.gz' > filename01 it will create a file with all of the results that show up? How do I make it so that the file is a .txt file? Do I just add > filename.txt?



Again, if it's not too much trouble, could you explain the lines of code so I can understand what those words/commands are doing? It's just that at first glance, it all seems a bit daunting so perhaps if I know the purpose of each line/word/character - I can get a better idea of how the command works.

If I just want to find the .bvec and .bval files separately (run the command twice) do I change the code as such?
Code:
find . -name '*DTISiemensTCless*.bvec' |
while read -r path
do	dir="${path%/*}"
	file="${path##*/}"
	subjectnumber="${file%%DTI*}"
	ext="${file##*.}"
	echo mv "$path" "$dir/${subjectnumber}DTI.$ext"
done

Thank you again!
Hi azurite,
First, a suggestion: Don't be afraid to try things and watch what happens!

And, another suggestion: Instead of searching the web to try to figure out what a utility on your system will do, read the manual page for that utility on your system. For instance, the command:
Code:
man find

will show you the manual page for the find utility on your system.

You still haven't told us what shell you're using. The manual page for your shell will describe pipelines, the while loop construct, the read built-in utility, variable assignments, parameter expansions, and file redirections. And, the mv manual page will describe how it can be used to rename files.

Unlike Windows, UNIX systems determine the type of a file by its contents; not by its name.

The command: find . -name '*eddy_corrected_brain.nii.gz*' searches the file hierarchy rooted in the current directory (.) for files with names that contain the string eddy_corrected_brain.nii.gz proceeded and followed by strings of 0 or more characters (*) and writes the pathname of each matching filename on a line by itself. Note that I did NOT include an asterisk at the end of the pattern I used in the find commands I suggested because I thought you only wanted filenames ending in .gz; not filenames containing .gz followed by other arbitrary text.

Since pathnames cannot contain NUL bytes, this output will be a text file unless the pathnames produced are longer than the number of bytes your system allows in a line in a text file. The number of types allowed on all UNIX and Linux systems is at least 2048. The actual number on your system can be found using the command:
Code:
getconf LINE_MAX

With no file redirection (as above) the output is displayed on your terminal. If you redirect the output as in any of the following:
Code:
find . -name '*eddy_corrected_brain.nii.gz' > file
find . -name '*eddy_corrected_brain.nii.gz' > file.txt
find . -name '*eddy_corrected_brain.nii.gz' > file.pdf

the output file will be a text file. By convention, storing text in a file with the filename extension .pdf is a VERY bad idea, but the name you choose for your output does not in any way affect the output placed in that file by the find utility.

If you want to see what a shell script is doing, turn on tracing and watch what it does while it is running. For example to add tracing to the script:
Code:
set -xv
find . -name '*DTISiemensTCless*.bvec' -o -name '*DTISiemensTCless*.bval' |
while read -r path
do	dir="${path%/*}"
	file="${path##*/}"
	subjectnumber="${file%%DTI*}"
	ext="${file##*.}"
	echo mv "$path" "$dir/${subjectnumber}DTI.$ext"
done
set +xv

The set -xv at the start of the script turns tracing on and the set +xv at the end of the script turns tracing off.

In the above pipeline, find prints the pathnames of all filenames containing the string DTISiemensTCless that end with the string .bvec or (-o) with the string .bval, the read command sets the variable path to the contents of one of those lines of output from find and the while loop contains parameter expansions and variable assignments that set dir to the name of the directory specified in path, file to the last component of path , subjectnumber to the part of that file's name before the string DTI, ext to the file's extension (not including the period), and the mv command renames the file named by that line of output from find to be a file in the same directory, with the same subject number that was in the original file followed by the string DTI followed by the filename extension that was on the original file.
This User Gave Thanks to Don Cragun For This Post:
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

finding duplicate files by size and finding pattern matching and its count

Hi, I have a challenging task,in which i have to find the duplicate files by its name and size,then i need to take anyone of the file.Then i need to open the file and find for more than one pattern and count of that pattern. Note:These are the samples of two files,but i can have more... (2 Replies)
Discussion started by: jerome Sukumar
2 Replies

2. Shell Programming and Scripting

Renaming the files

Hello, i wanna rename my files which names are written in movies.txt films.txt = amovie bmovie cmovie dmovie emovie and i wanna find this files and rename the files to 1_amovie ... (12 Replies)
Discussion started by: redbeard_06
12 Replies

3. Shell Programming and Scripting

renaming files, please help

I want to rename the files by taking part of the file and appending date to it. please help e.g. abc-390.csv xyz-908.csv desired format is abc_YYYYMMDD.csv This is what I have but it is not working for each in *.csv; do mv $each /abc/data/"`date '+test_%Y%M%M'`".csv done (2 Replies)
Discussion started by: mqasim
2 Replies

4. Shell Programming and Scripting

Finding files older than the current date and time and renaming and moving

Hi, I have a very urgent requirement here. I have to find all files in the specified directory but not in the sub directories(The directory name is stored in a variable) which are older than the current date as well as current time and rename it as filename_yyyymmddhhmmss.ext and move it into a... (7 Replies)
Discussion started by: ragavhere
7 Replies

5. Shell Programming and Scripting

renaming files

Hello, I wanted to rename one file where filename contains space.. How can i rename in unix? The file name is ABC XYZ.TXT I wanted to rename this file as ABCXYZ.TXT. Any help is greatly appreciated... Regards. (4 Replies)
Discussion started by: govindts
4 Replies

6. Shell Programming and Scripting

renaming files or adding a name in the beginning of all files in a folder

Hi All I have a folder that contains hundreds of file with a names 3.msa 4.msa 21.msa 6.msa 345.msa 456.msa 98.msa ... ... ... I need rename each of this file by adding "core_" in the begiining of each file such as core_3.msa core_4.msa core_21.msa (4 Replies)
Discussion started by: Lucky Ali
4 Replies

7. Shell Programming and Scripting

Renaming files

Hi i have to achieve the following i have files as xyz001.csv, xyz002.csv.......xyz0025.csv in a folder, i need to keep xyz001.csv as it is but want to remove the extra zero on filename from 10 say xyz0010 should be renamed to xyz010 xyz0025 should be renamed as xyz025 Note xyz... (8 Replies)
Discussion started by: mad_man12
8 Replies

8. Shell Programming and Scripting

Renaming files

Hello, I am looking for a command line that will rename name files : f700_abc_o_t_MASTERID_AS_AE_20130323.csv like this f700_abc_o_t_MASTERID_AS_AE_20130324.csv The great idea could be to get the date stamp 20130323 and change any part of it, instead of just change the... (4 Replies)
Discussion started by: Aswex
4 Replies

9. Shell Programming and Scripting

Renaming multiple files in sftp server in a get files script

Hi, In sftp script to get files, I have to rename all the files which I am picking. Rename command does not work here. Is there any way to do this? I am using #!/bin/ksh For eg: sftp user@host <<EOF cd /path get *.txt rename *.txt *.txt.done ... (7 Replies)
Discussion started by: jhilmil
7 Replies
All times are GMT -4. The time now is 09:01 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy