various examples of sed strings within a vi session
PROBLEM: copied files from another file system over to a different drive, but put them in /home instead of correct directory
1) first make sure that the files are owned by "fred"
2) then move the files to a sub-directory showing the old drive, in this case an old 250GB drive
3) note that sudo is first used to chown (change owner).
4) A relative path is chosen, this was run from /home/fred
5) to create the file, fred typed: ls ../ > clean
6) then fred opened the file using vi, vi clean
7) then fred typed this string:
:%s$.*$sudo chown -R fred:users ../& ; mv ../& 250GB-files$g
1) the first thing fred hit was to go into the command mode in vi
2) the next thing he typed was : (colon), this opened up the ex editor at the bottom
3) then he typed %s to indicate substitute ALL found in the search, the search delimited by $ (the default is / )
the search can be limited to specific lines, e.g. 1,15s or from line 16 to the end, 16,$s
4) fred used the $ delimiter because there were slashes (/) in the replacement string
5) after the first $ there is .* (period, star), this means any character, multiple instances (in other words, everthing on the line)
6) after the second $ the replacement string was typed... if there had been any $ in that string they would need to be "escaped" with a \
7) in the replacement string fred inserts the original line, represented by & - and inserts that into the actions he'll take.
8) after the replacement string fred adds the closing $ and g for global. YMMV in that some versions don't require this.
8) then fred hit return and the text above was replaced line by line with the following:
sudo chown -R fred:users ../BATCH_XP ; sudo mv ../BATCH_XP 250GB-files
sudo chown -R fred:users ../Misc_files ; sudo mv ../Misc_files 250GB-files
sudo chown -R fred:users ../Pictures ; sudo mv ../Pictures 250GB-files
sudo chown -R fred:users ../MP3s ; sudo mv ../MP3x 250GB-files
sudo chown -R fred:users ../OLD_320G_IDE-copy-4-2013 ; sudo mv ../OLD_320G_IDE-copy-4-2013 250GB-files
sudo chown -R fred:users ../penguin-artifacts ; sudo mv ../penguin-artifacts 250GB-files
9) fred then saved and exited vi by typing, :wq
10) from the command line, fred typed: sh ./clean
11) note, fred needed to be in /etc/sudoers to execute sudo, he may have to enter his password during this script.
-- Linux commands, scripts, tools and systems administration --