After I ls
and see the list, is there anyway to process a document without typing its name?
For example instead of typing rm damnSoLongFileName
, can I type something like rm [1]
?
I mean is there anyway to use ls
like array?
Here is a one liner:
for something in $( ls );do echo $something;done
Or using select
:
select item in $( ls );do echo $item;done
You could replace echo
with rm
and it would remove the item that you selected. You could then press ctrl+c to stop the loop.
Output:
bob@bob-p7-1298c:~$ select item in $( ls );do echo $item;done
1) adifferentlikethis 6) Documents 11) initramfs 16) MESVG20.xlsx 21) out.png 26) racket 31) t~ 36) Untitled 41) VMs
2) bash_speakit 7) Downloads 12) irssi_log 17) MLGVG17.xlsx 22) output.mp3 27) run.py 32) Templates 37) Document
3) color_img.jpg 8) ec2 13) likethis 18) MLRVG17.xlsx 23) php5 28) run.sh 33) test 38) 1
4) DATES 9) examples.desktop 14) lpr 19) Music 24) Pictures 29) space.txt 34) t.sh 39) Videos
5) Desktop 10) grub.iso 15) MEEVG19.xlsx 20) nano.save 25) Public 30) t 35) Untitled-2.pd 40) VirtualBox
#?
I then enter a number and it will echo it back to me.
#? 2
bash_speakit
Another Possibility:
thearray=( $(ls) )
echo "${thearray[2]}"
Output:
color_img.jpg
You could use tab-completion for long filenames. For your example type something like rm damn<TAB>
.
From the "I mean is there anyway to use ls like array?" part I would assume that the question is about simply streaming the ls results into some other command thus I'm replying with a link to Xargs and a simple example usage of it:
find -name '*.txt' -print0 | xargs -0 rm
Check man find
for more information about how to find, filter and stream the results using find
command and man xargs
for a full manual about xargs. The above example is a simple classic case of -print0 + -0
combination of arguments between the two using pipe.
No doubt unorthodox, but if you copy the script below into an empty file, save it as ls_2
in ~/bin
, make it executable, and run it by the command (from anywhere):
ls_2
it will show a list like:
jacob@jacob-System-Product-Name:~/Bureaublad/all kinds of crap$ ls_2
1. nog te doen.odt
2. otto
3. GW_site_werkmap
4. sanel
5. blacklist.2
6. sanel_edited
7. sanel_gigue.mp3
8. Sprachmemo_011[1].m4a
9. PRG001
10. crap
please enter numbers to remove (separated by a comma): 1,3,5
Just enter the numbers and the items (both directories and files) will be removed.
Like the "real" ls command, it can be run from the current directory (just ls_2
) or with a directory as argument (ls_2 /path/to/directory
).
#!/usr/bin/env python3
import os
import shutil
import sys
try:
currdir = sys.argv[1]
except IndexError:
currdir = os.getcwd()
items = os.listdir(currdir)
for i in range(len(items)):
print(str(i+1)+".", items[i])
remove = input("\nplease enter numbers to remove (separated by a comma): ")
indices = [int(it) for it in remove.split(",")]
for i in indices:
file = currdir+"/"+items[i-1]
try:
os.remove(file)
except IsADirectoryError:
shutil.rmtree(file)