B
B
BlagoYar Silence2021-06-15 12:52:19
linux
BlagoYar Silence, 2021-06-15 12:52:19

Is it possible to substitute the filename from ls?

Let's say there is an archive with a long name, can its name be somehow substituted into the current line from the list using ls?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
X
xotkot, 2021-06-15
@BlagoYar

the task is not quite clear
you can't copy with the mouse and auto-completion doesn't work?
find out the number of the desired line:
ls -A1 | nl'
if you need only visible (without a dot at the beginning of the name) files, then you can skip the A
key ; print the line by its number:
ls -A1 | awk 'NR==7'
where 7 is an example of the line number with the desired file name issued using the ls command, put your number.
if you wish, you can wrap it in a variable and just insert it where you need it:
f=$(ls -A1 | awk 'NR==7')
then you can already operate on this variable, for example:
rm "$f"
p.s.
if you need more interactive, you can use the skim (sk) utility or fzf
f=$(ls | fzf)
for hidden files, add the -A switch
will make it possible to select the desired file with the keys (top, bottom), and then (Enter) transfer it to a variable.
well, then we already operate with the variable f

A
Alexander, 2021-06-15
@shabelski89

#!/bin/bash
FILES="/root/temp/*"

#echo $FILES

for file in $FILES
do
if [ "${file: -4}" == ".zip" ]; then
 echo "some string with name of $file"
fi
done

We spend a couple of hours reading articles and you're done!

S
Saboteur, 2021-06-15
@saboteur_kiev

$ select file in $(ls -A1); do break;done
$ echo $file
blablabla.txt

run, after selecting a file it is in the $file variable
True, it works with a list of files that will fit into the length of the line (usually ~ 4 kb)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question