Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Rename file in directory using contents within each file Post 303042502 by Scrutinizer on Friday 27th of December 2019 07:44:32 AM
Old 12-27-2019
Hi Ravinder,

As long as a mv operation is performed on the same file system - as is the case here - that should not pose a problem, since mv then only manipulates directory data: A file name is nothing more than a directory entry, a pointer (a hard link) to the file itself.

When a process opens a file for reading, the operation system creates an entry (file descriptor) to represent that file and stores information about that opened file in memory. So then the directory entry is no longer used.

The mv operation is thus free to manipulate the directory entry.

So for the process that has opened and is reading the file, nothing changes as the directory data is being changed.
When it is done reading it just closes the file descriptor.

Also, the file list expanded by the glob is expanded before being passed to the awk script, so new file names are not passed to the script.

S.
These 2 Users Gave Thanks to Scrutinizer For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SED To insert Directory Contents to file

I am on a HP-UX machine I have a directory called "/u01/blobs" and the files look like this: ls -1 7398 7399 7400 I need to produce a comma delimited file with the following format: filename,location/filename i.e: 7398,/u01/blobs/7398 7399,/u01/blobs/7399 7400,/u01/blobs/7400 What... (3 Replies)
Discussion started by: NomDeGuerre
3 Replies

2. Shell Programming and Scripting

Read File and use contents to rename another

Hello guys, thank God that I found this forum. I hope that someone can help me because I don't have any idea on how to start it. I know that for some of you this is a very simple task but I'm not as advance on shell scripting like many people out there. I got this file with a permanent... (10 Replies)
Discussion started by: Shark Tek
10 Replies

3. Shell Programming and Scripting

A script that will move a file to a directory with the same name and then rename that file

Hello all. I am new to this forum (and somewhat new to UNIX / LINUX - I started using ubuntu 1 year ago).:b: I have the following problem that I have not been able to figure out how to take care of and I was wondering if anyone could help me out.:confused: I have all of my music stored in... (7 Replies)
Discussion started by: marcozd
7 Replies

4. UNIX for Dummies Questions & Answers

Help with searching for a file in a directory and copying the contents of that file in a new file

Hi guys, I am a newbie here :wall: I need a script that can search for a file in a directory and copy the contents of that file in a new file. Please help me. :confused: Thanks in advance~ (6 Replies)
Discussion started by: zel2zel
6 Replies

5. Shell Programming and Scripting

Rename multiple file names in a directory

I hope some one can help me I have multiple files in a directory with out extension like as below mentioned. But i want to change all the file names along .DDMMYYYYHHMISS format. And all files should have same DDMMYYYYHHMISS. Scenario: direcory name = /vol/best/srcfiles files in a... (4 Replies)
Discussion started by: hari001
4 Replies

6. Shell Programming and Scripting

Rename last directory in a file structure

I have to write a script to rename the every last sub-directory in a directory structure if the last sub-directory name doesn't contain "submitted". eg: given directory path:/u01/home/somedir somedir can have many subdirectories and each subdirectory inturn has many subdirectories. somedir... (3 Replies)
Discussion started by: ramse8pc
3 Replies

7. Shell Programming and Scripting

How to read contents in each file and rename the file?

Hello All, Can you help me in writing a script for reading the specific position data in a file and if that data found in that file that particular file should be renamed. Ex: Folder : C:\\test and Filename : CLSACK_112214.txt,CLSACK_112314.txt,CLSACK_112414.txt Contents in the file would... (3 Replies)
Discussion started by: nanduedi
3 Replies

8. Shell Programming and Scripting

Remove or rename based on contents of file

I am trying to use the two files shown below to either remove or rename contents in one of those files. If in file1.txt $5 matches $5 of file2.txt and the value in $1 of file1.txt is not "No Match" then that value is substituted for all values in $5 and $1 of file2.txt. If however in $1 ... (5 Replies)
Discussion started by: cmccabe
5 Replies

9. Shell Programming and Scripting

Rename specific file extension in directory with match to another file in bash

I have a specific set (all ending with .bam) of downloaded files in a directory /home/cmccabe/Desktop/NGS/API/2-15-2016. What I am trying to do is use a match to $2 in name to rename the downloaded files. To make things a more involved the date of the folder is unique and in the header of name... (1 Reply)
Discussion started by: cmccabe
1 Replies

10. UNIX for Beginners Questions & Answers

Rename the file name from Parent directory

Hi All, Just started learning unix and stuck into below issue. Suppose i have folder structure as below. Dir1/Dir2/Dir3/File1.msg I am looking to rename the file name from File1.msg to File2.msg but from the parent Dir1 From Dir3 i can easily run the command like mv File1.msg... (2 Replies)
Discussion started by: Gurjeet Singh
2 Replies
rename(2)							System Calls Manual							 rename(2)

NAME
rename - Renames a directory or a file within a file system SYNOPSIS
#include <stdio.h> int rename ( const char *from, const char *to ); [Tru64 UNIX] The following definition of the rename() function does not conform to current standards and is supported only for backward compatibility (see standards(5)): int rename ( char *from, char *to ); STANDARDS
Interfaces documented on this reference page conform to industry standards as follows: rename(): XSH5.0 Refer to the standards(5) reference page for more information about industry standards and associated tags. PARAMETERS
Identifies the file or directory to be renamed. Identifies the new pathname of the file or directory to be renamed. If the to parameter is an existing file or empty directory, it is replaced by the from parameter. If the to parameter is a nonempty directory, the rename() func- tion exits with an error. DESCRIPTION
The rename() function renames a directory or a file within a file system. For rename() to complete successfully, the calling process must have write and search permission to the parent directories of both the from and to parameters. If the from parameter is a directory and the parent directories of from and to are different, then the calling process must have write and search permission to the from parameter as well. If the from and to parameters both refer to the same existing file, the rename() function returns successfully and performs no other action. Both the from and to parameters must be of the same type (that is, both directories or both nondirectories) and must reside on the same file system. If the to parameter already exists, it is first removed. In this case it is guaranteed that a link named the to parameter will exist throughout the operation. This link refers to the file named by either the to or from parameter before the operation began. If the final component of the from parameter is a symbolic link, the symbolic link (not the file or directory to which it points) is renamed. If the final component of the to parameter is a symbolic link, the symbolic link is destroyed. If the from and to parameters name directories, the following must be true: The from parameter is not an ancestor of the to parameter. For example, the to pathname must not contain a path prefix that names from. The from parameter is well-formed. For example, the . (dot) entry in from, if it exists, refers to the same directory as from, exactly one directory has a link to from (excluding the self-referential . ), and the .. (dot-dot) entry in from, if it exists, refers to the directory that contains an entry for from. The to parameter, if it exists, must be well-formed (as defined previously). Upon successful completion, the rename() function marks the st_ctime and st_mtime fields of the parent directory of each file for update. RETURN VALUES
Upon successful completion, the rename() function returns a value of 0 (zero). Otherwise, a value of -1 is returned, and errno is set to indicate the error. ERRORS
If the rename() function fails, the file or directory name remains unchanged and errno may be set to one of the following values: Creating the requested link requires writing in a directory with a mode that denies write permission, or a component of either pathname denies search permission. The directory named by the from or to parameter is currently in use by the system or by another process. The directory that would contain to cannot be extended because the user's quota of disk blocks on the file system containing the directory is exhausted. The to parameter is an existing nonempty directory. Either the to or from parameter is an invalid address. Either the from or to parame- ter is not a well-formed directory, an attempt is made to rename . (dot) or .. (dot-dot), or the from parameter is an ancestor of the to parameter. [Tru64 UNIX] An I/O error occurred when updating the (underlined) directory. The to parameter names a directory and the from parameter names a nondirectory. Too many links were encountered in translating either to or from. The length of the to or from parameters exceeds PATH_MAX or a pathname component is longer than NAME_MAX. Indicates either that the system file table is full, or that there are too many files currently open in the system. A component of either path does not exist, or either path is the empty string, or the file named by the from parameter does not exist. The directory that would contain to cannot be extended because the file system is out of space. The from parameter names a directory and the to parameter names a nondirectory. The S_ISVTX flag is set on the directory contain- ing the file to be renamed, and the caller is not the file owner. The requested operation requires writing in a directory on a read-only file system. The link named by the to parameter and the file named by the from parameter are on different file systems. RELATED INFORMATION
Commands: chmod(1), mkdir(1), mv(1), mvdir(1) Functions: chmod(2), link(2), mkdir(2), rmdir(2), unlink(2) Standards: standards(5) delim off rename(2)
All times are GMT -4. The time now is 06:17 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy