Sed Use To Rename Files Under Multiple Directory

Hi all,

I am trying to use sed to rename multiple files under multiple directory.

so lets say for exmaple:
Under /root I have 2 directory as follows:

# ll {test1,test2}
test1:
total 0
-rw-r--r--. 1 root root 0 Apr 10 19:16 authkey.apollo

test2:
total 0
-rw-r--r--. 1 root root 0 Apr 10 19:16 authkeys.apollo

if I want to change apollo to jupiter then used this:
for i in `ls {test1,test2} | grep -i 'apollo'`; do echo $i; sed -i 's/apollo/jupiter/g'; done

but it seems like it got missed on the file path in sed. is there any other easier way or better approach to make this work?

Thanks in advance.


Similar Content



SFTP Configuration For Single Directory Per User

Dear Members,

I am trying to configure sftp on my Server (Redhat). But I am facing following issue.

Link used: https://www.linode.com/docs/tools-re...ian-and-ubuntu

These are the parameters added to /etc/ssh/sshd_config file :

Quote:
Match group filetransfer
ChrootDirectory %h
X11Forwarding no
AllowTcpForwarding no
ForceCommand internal-sftp
Permission home Directories are as follows :

Quote:
ls -ld /home/test1/
drwxr-xr-x 7 root root 4096 May 14 09:49 /home/test1/

ls -l /home/test1/
total 12
drwxr-xr-x 2 root root 4096 May 14 09:49 docs
drwxr-xr-x 2 root root 4096 May 13 11:43 nitish
drwxr-xr-x 2 root root 4096 May 14 09:49 public_html

Debug Command : sftp -vvv test1@Ipaddress

Code:
debug1: Next authentication method: password
test1@IP-address's password:
debug3: packet_send2: adding 64 (len 53 padlen 11 extra_pad 64)
debug2: we sent a password packet, wait for reply
debug3: Wrote 144 bytes for a total of 1421
debug1: Authentication succeeded (password).
debug2: fd 4 setting O_NONBLOCK
debug3: fd 5 is O_NONBLOCK
debug1: channel 0: new [client-session]
debug3: ssh_session2_open: channel_new: 0
debug2: channel 0: send open
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug3: Wrote 128 bytes for a total of 1549
debug3: Wrote -1 bytes for a total of 1549
Write failed: Broken pipe
Couldn't read packet: Connection reset by peer

I need the user can't access/see the other directories other than it's own directory

Waiting for your response.

Thank you

Redhat-Keeda

Connecting To Specific Directory With Filezilla

Hello, I have an embedded linux device. I can connect to the device and I can upload or download files. No problem with this. But, at first connection, I want to connect to "/" directory instead of "/root". In the device file system, there are files ssh_config and sshd_config under /etc/ssh directory. I think I have to do something with these files but I don't know what.

I don't exactly know what mean "/root" and "/" directories. I think that the real root directory is "/" directory which is empty but when I connect with filezilla, the "/root" directory is the default so I had to go back to / directory everytime.

Sed: Transforming 'ls-laR' Output Into A List With Absolute Paths

Hello, this is my first post
First i would like to thank you all for answering other people questions because I've been able to learn from the forum a lot.

I need your help with something.
I have standard output from 'ls -laR /etc' command which looks like this:
Code:
/etc/X11/xorg.conf.d:
total 4
drwxr-xr-x. 2 root root  29 Apr  1 00:46 .
drwxr-xr-x. 5 root root  54 Apr  1 00:43 ..
-rw-r--r--. 1 root root 232 Apr  1 00:46 00-keyboard.conf

/etc/xdg:
total 12
drwxr-xr-x.  4 root root   36 Apr  1 00:43 .
drwxr-xr-x. 87 root root 8192 Apr 12 13:53 ..
drwxr-xr-x.  2 root root    6 Jun 10  2014 autostart
drwxr-xr-x.  2 root root   17 Apr  7 01:25 systemd

by using sed command:
Code:
sed -e '/./!d' -e '/^total/d' -e '/\.$/d' -e 's/:$/\//' list.txt

I have transformed it to the following form:

Code:
/etc/X11/xorg.conf.d/
-rw-r--r--. 1 root root 232 Apr  1 00:46 00-keyboard.conf
/etc/xdg/
drwxr-xr-x.  2 root root    6 Jun 10  2014 autostart
drwxr-xr-x.  2 root root   17 Apr  7 01:25 systemd

and now I would like to achieve absolute paths at the end of each row

Code:
-rw-r--r--.  1 root root  232 Apr  1 00:46 /etc/X11/xorg.conf.d/00-keyboard.conf
drwxr-xr-x.  2 root root    6 Jun 10  2014 /etc/xdg/autostart
drwxr-xr-x.  2 root root   17 Apr  7 01:25 /etc/xdg/systemd


How do I join(merge) filenames with corresponding absolute path to their parent directory?



I know how to extract filenames using awk and get this:
Code:
00-keyboard.conf

autostart
systemd

but I don't know what to do next. Should I use some hitech sed option or go for loop or try with arrays? Help. Heeeelp

Space Disk "used" In Df Is Nowhere To Be Found With Du

Hello,

I am facing an issue with a filesystem (/dev/sda3); I see space used on it (around 365GB) when I am looking at the host with "df -h" command.

Code:
[root@srv_omega /]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3             443G  365G   56G  87% /
tmpfs                  95G   56K   95G   1% /dev/shm
/dev/sda1             484M   39M  421M   9% /boot
/dev/sdb1             3.6T  1.3T  2.2T  36% /hadoop/disk1
/dev/sdc1             3.6T  1.3T  2.2T  37% /hadoop/disk2
/dev/sdd1             3.6T  1.3T  2.2T  36% /hadoop/disk3
/dev/sde1             3.6T  1.3T  2.2T  37% /hadoop/disk4
/dev/sdf1             3.6T  1.3T  2.2T  36% /hadoop/disk5
/dev/sdg1             3.6T  1.3T  2.2T  36% /hadoop/disk6
/dev/sdh1             3.6T  1.3T  2.2T  36% /hadoop/disk7
/dev/sdi1             3.6T  1.3T  2.2T  36% /hadoop/disk8
/dev/sdj1             3.6T  1.3T  2.2T  36% /hadoop/disk9
/dev/sdk1             3.6T  1.3T  2.2T  36% /hadoop/disk10
/dev/sdl1             3.6T  1.2T  2.3T  36% /hadoop/disk11
/dev/sdm1             3.6T  1.3T  2.2T  36% /hadoop/disk12
/dev/sdn1             3.6T  1.3T  2.2T  36% /hadoop/disk13
/dev/sdo1             3.6T  1.3T  2.2T  37% /hadoop/disk14
/dev/sdp1             3.6T  1.1T  2.4T  30% /hadoop/disk15
cm_processes           95G  8.2M   95G   1% /var/run/cloudera-scm-agent/process

I have looked if any hidden file might cause the issue, no joy.

Code:
[root@srv_omega /]# pwd
/
[root@srv_omega /]#  ls -lrtha
total 121K
drwxr-xr-x    2 root root 4.0K Jun 28  2011 srv
drwxr-xr-x    2 root root 4.0K Jun 28  2011 mnt
drwxr-xr-x    2 root root 4.0K Jun 28  2011 media
drwxr-xr-x    2 root root 4.0K Dec 20  2012 cgroup
drwx------    2 root root  16K Jun  2  2014 lost+found
drwxr-xr-x    2 root root 4.0K Jun  2  2014 selinux
-rw-r--r--    1 root root    0 Jun  3  2014 .autorelabel
drwxr-xr-x   18 root root 4.0K Jun  5  2014 hadoop
drwxr-xr-x   21 root root 4.0K Jun  5  2014 var
dr-xr-xr-x    9 root root  12K Jun 20  2014 lib64
dr-xr-xr-x    2 root root  12K Jun 21  2014 sbin
dr-xr-xr-x    2 root root 4.0K Jun 21  2014 bin
dr-xr-xr-x    5 root root 1.0K Jun 22  2014 boot
dr-xr-x---    5 root root 4.0K Jun 22  2014 root
drwxr-xr-x    6 root root 4.0K Jun 22  2014 opt
drwxr-xr-x    3 root root 4.0K Dec 10 19:11 home
dr-xr-xr-x   13 root root 4.0K Dec 12 16:18 lib
dr-xr-xr-x 1140 root root    0 Apr 30 15:11 proc
drwxr-xr-x   13 root root    0 Apr 30 15:11 sys
-rw-r--r--    1 root root    0 Apr 30 15:11 .autofsck
drwxr-xr-x    2 root root    0 Apr 30 15:11 misc
drwxr-xr-x    2 root root    0 Apr 30 15:11 net
drwxr-xr-x   15 root root 4.0K Apr 30 15:12 usr
drwxr-xr-x   19 root root 4.6K Apr 30 15:12 dev
dr-xr-xr-x   27 root root 4.0K Apr 30 15:12 ..
dr-xr-xr-x   27 root root 4.0K Apr 30 15:12 .
drwxr-xr-x  122 root root  12K May  4 03:33 etc
drwxrwxrwt   16 root root 4.0K May  7 06:14 tmp

So I try to find where the space is used with a "du -sh" command

Code:
[root@srv_omega /]# pwd
/
[root@srv_omega /]# du -sh *
7.8M    bin
29M     boot
4.0K    cgroup
280K    dev
26M     etc
19T     hadoop
124K    home
144M    lib
26M     lib64
16K     lost+found
4.0K    media
0       misc
4.0K    mnt
0       net
7.9G    opt
du: cannot access `proc/9170/task/27326/fdinfo/538': No such file or directory
du: cannot access `proc/45119/task/45119/fd/4': No such file or directory
du: cannot access `proc/45119/task/45119/fdinfo/4': No such file or directory
du: cannot access `proc/45119/fd/4': No such file or directory
du: cannot access `proc/45119/fdinfo/4': No such file or directory
du: cannot access `proc/45160': No such file or directory
0       proc
3.8M    root
17M     sbin
4.0K    selinux
4.0K    srv
0       sys
3.9M    tmp
2.6G    usr
16G     var

So as far as I understand, only /hadoop is a suitable suspect (as cumulative size of all the other folders on "/" are well below the 365GB)

Code:
[root@srv_omega hadoop]# cd /
[root@srv_omega /]# cd /hadoop
[root@srv_omega hadoop]# ls -lrtha
total 72K
drwxr-xr-x  2 root root 4.0K Jun  5  2014 disk16
drwxr-xr-x 18 root root 4.0K Jun  5  2014 .
drwxr-xr-x  4 root root 4.0K Jun 22  2014 disk1
drwxr-xr-x  4 root root 4.0K Jun 22  2014 disk11
drwxr-xr-x  4 root root 4.0K Jun 22  2014 disk10
drwxr-xr-x  4 root root 4.0K Jun 22  2014 disk13
drwxr-xr-x  4 root root 4.0K Jun 22  2014 disk12
drwxr-xr-x  4 root root 4.0K Jun 22  2014 disk14
drwxr-xr-x  4 root root 4.0K Jun 22  2014 disk2
drwxr-xr-x  4 root root 4.0K Jun 22  2014 disk4
drwxr-xr-x  4 root root 4.0K Jun 22  2014 disk3
drwxr-xr-x  4 root root 4.0K Jun 22  2014 disk6
drwxr-xr-x  4 root root 4.0K Jun 22  2014 disk5
drwxr-xr-x  4 root root 4.0K Jun 22  2014 disk8
drwxr-xr-x  4 root root 4.0K Jun 22  2014 disk7
drwxr-xr-x  4 root root 4.0K Jun 22  2014 disk9
drwxr-xr-x  5 root root 4.0K Nov 19 20:02 disk15
dr-xr-xr-x 27 root root 4.0K Apr 30 15:12 ..

All folders from 1 to 15 are on different filesystems, so the folder disk16 seems to be the only option but there is nothing in it.

Code:
[root@srv_omega hadoop]# cd disk16/
[root@srv_omega disk16]# ls -lrtha
total 8.0K
drwxr-xr-x 18 root root 4.0K Jun  5  2014 ..
drwxr-xr-x  2 root root 4.0K Jun  5  2014 .
[root@srv_omega disk16]#

I just don't get it; no folder seems responsible for the "365Gb"...

Any idea on how I could try to find out where those "365GB" are ?

Added 'exit 0' To The Bottom Of .bashrc, Now What?

Centos 6.5

I ran into an interesting problem (on reddit) that I figured I could solve, but I have not been able to. Its simple,.. I added 'exit 0' to /root/.bashrc, and now I am trying to log in via ssh.

Everytime I do, it immediately exits when it runs the .bash_profile, which sources .bashrc, (which is immediate upon 'logging in')

I've tried:

Code:
ssh root@192.168.1.50 -t vim
vim scp://192.168.1.50/.bashrc
vim scp://192.168.1.50/root/.bashrc
ssh root@192.168.1.50 bash --norc
ssh root@192.168.1.50 /bin/bash --norc --noprofile
ssh -T root@192.168.1.50 "mv /root/.bashrc /root/.bashRC"
scp .bashrc root@192.168.1.50:/root/
ssh root@192.168.1.50 /bin/bash --norc --noprofile -vvvvvvvvvv
ssh -vvvvvv root@192.168.1.50 /bin/bash --norc --noprofile
ssh -vvvvvv root@192.168.1.50 /bin/mv /root/.bashrc /root/.bashRC
ssh -t -t root@192.168.1.50 << EOF
mv /root/.bashrc /root/.bashRC
EOF
ssh -t -t root@192.168.1.50 --norc << EOF
echo HELLO > /root/.bashrc
EOF
ssh -tv root@192.168.1.50 rm .bashrc

So,.. I am unable to get back into the system (as root, no other users exist) after adding 'exit 0' to .bashrc

Anyone feel like explaining why all of these failed (aside from saying SSH interactive logins run the .bash_profile/.bashrc files) or, offering a suggestion that works? Seems like if you have the root password, you should be allowed to modify the login process... since... you know... you are root.

Linux Bulk Renaming Files

Hello Folks.

I'm searching for a easy way to rename multiple files from CLI but didn't find any easy way for me so I'm reaching out to you guys for help.

This is what I want to do (from CLII or script). I want to move files with a sequence number on the name of the files (msg0000, msg0001, msg0002 and so on) to let's say msg0066, msg0067 and so on. Each of this file name has two other files (msg0000.wav, msg0000.WAV and msg0000.txt).

The idea is to move them from one directory to another and following a sequence in the file names, is there a way I can do this pain free?

Any help on this matter will be greatly appreciates and I'm talking about over 100 files I need to move following the sequence of the receiving directory.

Thanks!

Chown Question For Subdirectories

I have a directory structure and files I wish to change permissions on, but I don't want to change permissions on every file/subfolder along the way. Example: I want to chown all files in dir4 where my directory struture is:
/dir1/dir2/dir3/dir4/*
However, both dir3 and dir4 are owned by root:root so I need to change the ownership on those directories as well. But, if I do:
chown -R user:group /dir1/dir2/dir3/dir4/
...it will indeed make dir4 and it's files accessible, but it doesn't change dir3 - so folks still can't get to the files they need. I thought about doing:
chown -R user:group /dir1/dir2/dir3*
... but that will give users access not only to dir4 and it's files, but any other files under dir3 which I don't want.

My work around has been to do a chown on /dir1/dir2/dir3 and then a second chown on /dir1/dir2/dir3/dir4/, but I am thinking there has to be a way to do what I want in a single chown command - right?

Setuid, Setgid, Sticky Bit

There's something really weird happening with permissions on a directory in linux. I have the directory /dir

Code:
ls -ld /dir
drwxrwxrwx. 2 root root 4096 Jan  8 00:06 dir
chmod 2777 /dir [this sets the setgid bit on]
ls -ld /dir
drwxrwsrwx. 2 root root 4096 Jan  8 00:06 /dir

And this is what I expect it to do.
However, when I change it to setid (WITHOUT setgid), it simply doesn't work. Instead of overwriting, it cumulates:
Code:
chmod 4777 /dir [for setting setuid bit on]
drwsrwsrwx. 2 root root 4096 Jan  8 00:06 /dir

As you can see, the setgid bit is still on together with the setuid! However, if I run:
Code:
chmod -s /dir

then the file has a 0777 permission.

And then again, if I change its permission to 4777 (so setuid):
Code:
chmod 4777 /dir
drwsrwxrwx. 2 root root 4096 Jan  8 00:06 /dir

it acts normally. So the same chmod 4777 acts differently in two different contexts.

I really can't understand it. Basically, I cannot get rid of the setuid/setgid through binary permissions. The sticky bit works fine. Changing from 1777 to 0777 adds and removes "t" respectively. What am I to make of all this? This behaviour makes no sense to make especially given that we can change the permissions to 6777 (setuid + setgid).

Script Rename Files

Dears,
I want to create script to rename multiple file names from unix date to date for example
filename_1421907815_department.txt
rename to
filename_2015_01_22_08_23_department.txt

Bash Script To Output Certain Lines From A File

I am trying to write a script to create a variable (string) out of specific lines from another file. The string will be used as headings for lists in another script. My plan is to create a variable "myvar" with the output from an awk command run against a file called banners.txt then I can call $myvar whenever it is appropriate in the larger script. additionally I can add/modify the banner file instead of having to comb through a large script to add or change echo commands.
So, here is my proof of concept script:
Code:
#!/bin/bash

path=/home/rstenger/

TESTFILE=$(<banners.txt)
MYVAR=$( awk 'NR > 6 && NR < 10' "$TESTFILE" )
#
echo ""
echo ""
echo $MYVAR
#
for i in {1..5}; do echo ""; done
echo $TESTFILE

exit

which produces this output:
Code:
sl7jump1~#->./test1.sh 
awk: cmd. line:2: fatal: cannot open file `******************************************************
*                1st banner has 3 line               *
******************************************************
******************************************************
*                2nd banner has 3 line               *
****************************************************** 
******************************************************
*                3rd banner has 3 line               *
******************************************************' for reading (File name too long)








3rd_floor_lab_ex3200s.txt banner banners.txt ban_test.sh base_configs Images lab_cyclades.sh lab_devices.v3.sh Projects savvis.lab_device_list Savvis_Lab-Domain-DNS.txt test1.sh vrf_names 3rd_floor_lab_ex3200s.txt banner banners.txt ban_test.sh base_configs Images lab_cyclades.sh lab_devices.v3.sh Projects savvis.lab_device_list Savvis_Lab-Domain-DNS.txt test1.sh vrf_names 1st banner has 3 line 3rd_floor_lab_ex3200s.txt banner banners.txt ban_test.sh base_configs Images lab_cyclades.sh lab_devices.v3.sh Projects savvis.lab_device_list Savvis_Lab-Domain-DNS.txt test1.sh vrf_names 3rd_floor_lab_ex3200s.txt banner banners.txt ban_test.sh base_configs Images lab_cyclades.sh lab_devices.v3.sh Projects savvis.lab_device_list Savvis_Lab-Domain-DNS.txt test1.sh vrf_names 3rd_floor_lab_ex3200s.txt banner banners.txt ban_test.sh base_configs Images lab_cyclades.sh lab_devices.v3.sh Projects savvis.lab_device_list Savvis_Lab-Domain-DNS.txt test1.sh vrf_names 3rd_floor_lab_ex3200s.txt banner banners.txt ban_test.sh base_configs Images lab_cyclades.sh lab_devices.v3.sh Projects savvis.lab_device_list Savvis_Lab-Domain-DNS.txt test1.sh vrf_names 2nd banner has 3 line 3rd_floor_lab_ex3200s.txt banner banners.txt ban_test.sh base_configs Images lab_cyclades.sh lab_devices.v3.sh Projects savvis.lab_device_list Savvis_Lab-Domain-DNS.txt test1.sh vrf_names 3rd_floor_lab_ex3200s.txt banner banners.txt ban_test.sh base_configs Images lab_cyclades.sh lab_devices.v3.sh Projects savvis.lab_device_list Savvis_Lab-Domain-DNS.txt test1.sh vrf_names 3rd_floor_lab_ex3200s.txt banner banners.txt ban_test.sh base_configs Images lab_cyclades.sh lab_devices.v3.sh Projects savvis.lab_device_list Savvis_Lab-Domain-DNS.txt test1.sh vrf_names 3rd_floor_lab_ex3200s.txt banner banners.txt ban_test.sh base_configs Images lab_cyclades.sh lab_devices.v3.sh Projects savvis.lab_device_list Savvis_Lab-Domain-DNS.txt test1.sh vrf_names 3rd banner has 3 line 3rd_floor_lab_ex3200s.txt banner banners.txt ban_test.sh base_configs Images lab_cyclades.sh lab_devices.v3.sh Projects savvis.lab_device_list Savvis_Lab-Domain-DNS.txt test1.sh vrf_names 3rd_floor_lab_ex3200s.txt banner banners.txt ban_test.sh base_configs Images lab_cyclades.sh lab_devices.v3.sh Projects savvis.lab_device_list Savvis_Lab-Domain-DNS.txt test1.sh vrf_names

What I was looking for is:
Code:
******************************************************
*                3rd banner has 3 line               *
******************************************************

I am very puzzled as to why I get the contents of the file in the awk error message, and the contents of my home directory as the variable.

Thanks,
Robert