I am trying to create spec files for my packages.
To list files I am using find command and redirecting it to a file:
readonly TOPDIR=`pwd`
cd package_folder
find . -iname >>$TOPDIR/list_files
Now my problem is I need to add prefix %dir for every directory otherwise I get file listed twice error.
Suppose content of my file is:
$ cat list_files
/bin
/bin/file_a
/bin/my_folder
/bin/my_folder/file_b
What I did was looked for dir’s
for file_name in `cat $TOPDIR/list_files`
do
chk_1=`ls -l $file_name |grep "^d"`
if [ "chk_1" ] ;then
sed -i "s@^$file_name@%dir $file_name@" $TOPDIR/list_files
fi
done
Now my output file(list_files) looks like this:
%dir /bin
%dir /bin/file_a
%dir /bin/my_folder
%dir /bin/my_folder/file_b
Reason being /bin is a directory so it replaces /bin with %dir.
Well, it’s kinda strange as I haven’t given @g option but still it’s replacing all /bin.
Is there any way to include "\n" in sed?
Using your current technique, make sure that the pattern matches the whole line with
$to mark the end of the line:The backslash is needed to prevent the shell from expanding
$@;seddoes not see the backslash.