Please make sure to read the alternative answer. It's even more to the point although not voted as high at this point.
You can use this to delete all symbolic links:
find -type l -delete
with modern find
versions.
On older find versions it may have to be:
find -type l -exec rm {} \;
# or
find -type l -exec unlink {} \;
To limit to a certain link target, assuming none of the paths contain any newline character:
find -type l | while IFS= read -r lnkname; do if [ "$(readlink '$lnkname')" == "/your/exact/path" ]; then rm -- "$lnkname"; fi; done
or nicely formatted
find -type l |
while IFS= read -r lnkname;
do
if [ "$(readlink '$lnkname')" = "/your/exact/path" ];
then
rm -- "$lnkname"
fi
done
The if
could of course also include a more complex condition such as matching a pattern with grep
.
Tailored to your case:
find -type l | while IFS= read -r lnk; do if (readlink "$lnk" | grep -q '^/usr/local/texlive/'); then rm "$lnk"; fi; done
or nicely formatted:
find -type l | while IFS= read -r lnk
do
if readlink "$lnk" | grep -q '^/usr/local/texlive/'
then
rm "$lnk"
fi
done
find
supports a lot of date input formats. The simplest format to obtain is YYYYMMDD HH:MM:SS. You already have the digits in the right order, all you have to do is extract the first group (${timestamp%??????}
: take all but the last 6 characters; ${timestamp#????????}
: take all but the first 8 characters), and keep going, appending punctuation then the next group as you go along.
timestamp=20130207003851
timestring=${timestamp%??????}; timestamp=${timestamp#????????}
timestring="$timestring ${timestamp%????}"; timestamp=${timestamp#??}
timestring="$timestring:${timestamp%??}:${timestamp#??}"
In bash (and ksh and zsh), but not in ash, you can use the more readable ${STRING_VARIABLE:OFFSET:LENGTH}
construct.
timestring="${timestamp:0:8} ${timestamp:8:2}:${timestamp:10:2}:${timestamp:12:2}"
To sort files by date, print out the file names preceded by the dates and sort that, then strip the date prefix. Use -printf
to control the output format. %TX
prints a part of the modification time determined by X
; if X
is @
, you get the number of seconds since the Unix epoch. Below I print three tab-separated columns: the time in sortable format, the file name, and the time in human-readable format; cut -f 2-
removes the first column and the call to expand
replaces the tab by enough spaces to accommodate all expected file names (adjust 40 as desired). This code assumes you have no newlines or tabs in file names.
find -maxdepth 1 -type f \
-newermt "$timestring" -printf '%T@\t%f\t%Tb %Td %TH:%TM\n' |
sort -k1n |
cut -f 2- |
expand -t 40
Best Answer
You might want to use
find -newermt
.Make sure to review files to be removed first:
Use
-delete
to perform actual removes.