linux "lessPATH" similar to $CDPATH and $PATH [closed] - linux

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
I like the flexibility and how easy it is to execute commands from any directory in $PATH variable or $CDPATH to change directory.
But is there an easy way to "less or open a file" from a list of directories you often visit.
Say you have log files in a different dir and other frequently visited directories in another. A less file1 command should look for the file1 in a list of directories defined like a $PATH or $CDPATH variable.

I understand that you look for an existing solution but if you use bash you can write a function than has exactly this behavior :
put in ~/.bashrc:
less2()
{
if [ $# -eq 0 ]; then
echo 'Missing filename ("less --help" for help)'
return 1
fi
if [ "$1" == "--help" ]
then
less $1
return 1
fi
OLDIFS=$IFS
IFS=':'
if [ -z $LESSPATH ]; then
SEARCH_PATHS=.
else
SEARCH_PATHS=.:${LESSPATH}
fi
for dir in $SEARCH_PATHS
do
if test -e "$dir/$1"
then
less "$dir/$1"
IFS=$OLDIFS
return
fi
done
IFS=$OLDIFS
echo "$1: No such file or directory"
}
You need to execute source ~/.bashrc in order to get less2 in your bash.
How to use the script
By defaults it looks for files in a current directory. If you set the enviroment variable LESSPATH less2 will look for a file first in the current directory and then if it is not there it will look for the file in all the directories in $LESSPATH. less2 is a function in a current bash process so it is not necessary to export LESSPATH, but of course you can also export LESSPATH.
$ less2 my_file.log
$ LESSPATH=path1:path2:path3
$ less2 my_other_file.log

Related

How to reset bash config? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 2 years ago.
Improve this question
Previously, when I mistakenly entered a wrong command in the terminal, I would get an output like this:
Command 'whoaim' not found, did you mean:
command 'whoami' from deb coreutils (8.30-3ubuntu2)
Try: sudo apt install <deb name>
but now when I enter a wrong command like whoaim instead of whoami I get this output:
bash: whoaim: command not found
how should I reset bash config?
You can reset the bash profile of user using following commands:-
Overwrite the existing .bashrc from user's home directory.
cp /etc/skel/.bashrc ~/
source ~/.bashrc
Take backup of existing .bashrc from user's home directory
However, check here how you can use the utility command-not-found in ubuntu
You would need to call a function that gives bash this capability. It's typically called command_not_found_handle. First, append it to your .bashrc file. Here is the code:
command_not_found_handle ()
{
if [ -x /usr/lib/command-not-found ]; then
/usr/lib/command-not-found -- "$1";
return $?;
else
if [ -x /usr/share/command-not-found/command-not-found ]; then
/usr/share/command-not-found/command-not-found -- "$1";
return $?;
else
printf "%s: command not found\n" "$1" 1>&2;
return 127;
fi;
fi
}
And then run this command to reload the profile file:
. ~/.bashrc

Find unix tree directory without the list of files [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 5 years ago.
Improve this question
I am looking to see the tree structure of a main directory on unix but without the files. "Tree" directory provides me the directory + files. How can i do directory only?
Here is bash script that recursively enters every directory starting from the current and prints its name:
#!/bin/bash
function loop {
echo "$pad"${PWD##*/}
for i in *
do
if [ -d "$i" ] && [ -x "$i" ]
then
cd "$i"
pad="$pad"$'\t'
loop
fi
done
pad=${pad#$'\t'}
cd ..
}
loop
Test dir structure (using #User123's answer:
$ find . -type d
.
./test
./foo
./foo/456
./foo/123
and scripts output:
$ bash ../test.sh
test
foo
123
456
test
Can you try this simple find command to see directories under main directory:
find . -type d

How to use 'history-c' command in a bash script? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 7 years ago.
Improve this question
As we know, 'history' command displays the command line history of Linux server and 'history -c' is the command to clear/delete this command line history.
I have to trigger this command through my bash script. Script is as follows,
#! /bin/bash
var=`history -c`
if [ $? -eq 0 ]
then
echo "cleared"
echo $var
fi
Output is as follows:
cleared
Though its printing "cleared" as the output, history-c is not deleting the history.
It would be great if you can guide/suggest on how i can achieve this, i.e using "history-c" command in my bahs script to delete command line history.Or is there any other way in which i can delete command line history through my bash script.
Thanks & Regards,
Navya
history -c clears the history for the current shell, and does not delete ~/.bash_history.
But when you run a script the current shell creates a new shell to run the script in and exits that shell when the script is done.
Instead, to execute a script in the current shell you have to source the script. If the name of your script is foo.sh, try running . ./foo.sh
But in either case, the script that you've written does not execute the command. Modify it to something like this:
#! /bin/bash
history -c
if [ $? -eq 0 ]
then
echo "cleared"
fi

rename multiple filename in linux [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
I have many files named xxxx.min.js and want to rename the files to xxxx.js so basically want to remove .min only.
Is there a command I can use to do this job?
I thought using rename command would be easy for each single file, but that would take forever since I have many of them.
any idea?
Here's a bash-only command (not requiring Perl)
for i in *.min.js; do a=$(basename $i .min.js); echo mv $i $a.js; done
Explanation
for i in *.min.js; do
loop over all files matching *.min.js
a=$(basename $i .min.js)
extract the base name of the file (i.e. strip off .min.js) and save the result in $a
echo mv $i $a.js
for now, print to the console the command that WOULD be run if you removed the echo
When you are satisfied that it generates the correct commands, remove the echo to actually rename the files.
Ubuntu and Debian linux distribution both have a perl version of mv function called rename or prename, which supports regexp. The manual can be found here.
Go to the folder of the files and run the command as follows:
rename s/\.min\.js$/\.js/ *.min.js

Linux - update multiple symlinks with a single command [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
When cloning an environment that utilizes symlinks, the symlinks are copied (from production to clone) but they are still pointing to the original (production) files. I want them to be pointing to the cloned files, like:
Original:
/production/symlink1 > /production/directory/file1
/production/foo/symlink2 > /production/directory/sub/file2
After clone (now):
/clone/symlink1 > /production/directory/file1
/clone/foo/symlink2 > /production/directory/sub/file2
I want:
/clone/symlink1 > /clone/directory/file1
/clone/foo/symlink2 > /clone/directory/sub/file2
Is there a way to achieve it with a single command?
Have you created the links yourself? If yes, you could create them with -r parameter. See man ln:
-r, --relative
create symbolic links relative to link location
If the links were pointing to absolute paths, they will always point to the same paths after copying. That's the beauty of absolute paths and I don't think you can work around that.
You can try to rewrite those links after copying though. For example if you create a script relink.sh like this:
#!/bin/bash
for link; do
target=$(readlink "$link")
[[ $target =~ ^/production ]] || continue
newtarget=$(echo $target | sed -e s?/production?/clone?)
echo ln -snf "$newtarget" "$link"
done
Pass a list of symlinks to this script, it will check if they are pointing to some path under /production, and recreate the link with /production replaced with /clone. You can call it like this for example:
find /clone -type l -exec ./relink.sh {} \;

Resources