-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy patheat.fish
More file actions
41 lines (31 loc) · 1.09 KB
/
eat.fish
File metadata and controls
41 lines (31 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
function eat --argument _dir
set dir (echo $_dir | trim-trailing-slash)
set files_to_move (find $dir -maxdepth 1 -not -path $dir)
for f in $files_to_move
set filename (echo $f | string replace $dir/ '')
# Handle the special case of a file within a directory named
# the same as the directory itself. Since the directory will
# be deleted, it won't collide with the file of the same name.
if equals $filename $dir
continue
end
if test -e ./$filename
error "eat: file would be overwritten: ./$filename"
return 1
end
end
set target (dirname $dir)
set tmpdir (mkusertemp)
for f in $files_to_move
mv -n $f $tmpdir
end
rmdir $dir || return $status
for f in $files_to_move
# Use the -n flag to not overwrite.
# This should already be handled by the `test -e` logic above
# but I'll also use -n here just to be safe.
set filename (echo $f | string replace $dir/ '')
mv -n $tmpdir/$filename $target
end
rmdir $tmpdir
end